• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 UINavigationController 中添加 UISegmentedControl 作为 titleView

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:20:21 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试以编程方式将 UISegmentedControl 添加为 UINavigationController 中的 titleView。但它没有出现。经过进一步调查,我发现如果根据 Apple docs 未将 leftBarButtonItem 设置为 nil,则会忽略 titleView 属性。 .所以我将它设置为 nil 但分段控件仍然没有显示!

下面是我的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = nil;
    UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects"ersonnal", @"Department", @"Company", nil]];
    [statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
    self.navigationItem.titleView = statFilter;
}

我该怎么办?我在这里错过了什么?

谢谢。

编辑:

感谢所有回复。但是,它们都不适合我,所以我进行了更深入的研究。这一定是我布置申请的方式。我可能应该早点提到这一点。道歉。

我正在创建的是一个 Storyboard 应用程序。主 Controller 是一个 UITabBarController。单击某个选项卡时,我想显示一个 UITableViewController。由于您无法在 UITableViewController 中显示导航栏,我发现我必须添加一个 UINavigationController 并在其中实现一个 UITableViewController。这就是我的应用的基本外观,

enter image description here

仪表板 View Controller 连接到一个继承自 UINavigationController 的类 类(class)。也许这就是问题所在?

因为我刚刚创建了一个小型测试应用程序,所以将 UIViewController 嵌入到 UINavigationController 中,并将我在上面的原始问题中发布的代码写在 UIViewController 的 ViewDidLoad 和分段控件显示得很好。现在我不知道如何在我的主应用程序中做同样的事情,因为没有其他 View Controller 或任何连接到导航 Controller 的东西。

对不起,如果我让事情变得更糟。如果有什么不清楚的地方请评论。



Best Answer-推荐答案


我猜你必须将 UISegmentedControll 设为 UIBarButtonItem:

更新: 此代码使 SegmentedControll 居中:

- (void)viewDidLoad{
    self.title = nil;
    UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects"ersonnal", @"Department", @"Company", nil]];
    [statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];

    NSMutableArray *buttonArray     = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexibleSpace  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [buttonArray addObject:flexibleSpace];
    [buttonArray addObject:[[UIBarButtonItem alloc] initWithCustomView:statFilter]];
    [buttonArray addObject:flexibleSpace];

    self.navigationItem.leftBarButtonItems = buttonArray;
    [buttonArray release];
}

希望对你有帮助!

关于ios - 在 UINavigationController 中添加 UISegmentedControl 作为 titleView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15199383/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap