在 navigation 添加 居中 的 segmentControl

如题,在navigationItem中添加一个UISegmentedControl,代替title。


直接上代码:

// 方法一:

UIView *titleBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 199, 31)];

titleBGView.backgroundColor = [UIColor clearColor];

self.navigationItem.titleView = titleBGView;    // 先给navigation一个titleView

segCon = [[UISegmentedControl alloc] initWithFrame:CGRectMake(3, 1, 193, 29)]; // segCon , 全局

for (int i = 0; i < titleArr.count; i++) {

[segCon insertSegmentWithTitle:titleArr[i] atIndex:i animated:YES];

}

segCon.momentary = NO; // 保持选定item,yes为不保持

segCon.multipleTouchEnabled = NO;

[segCon addTarget:self action:@selector(segConSelectedMethod:) forControlEvents:UIControlEventValueChanged];

[segCon setSelectedSegmentIndex:0];

//    [self.navigationItem setTitleView:segCon];  // 此方法无效

[self.navigationItem.titleView addSubview:segCon];  

// 方法二:

[self.navigationController.navigationBar.topItem setTitleView:segCon];

那么同理,你也可以把自定义的view添加到 titleView 。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,585评论 2 4
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 832评论 0 0
  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 2,426评论 2 50
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,141评论 5 13
  • 4.18第三天 吃完午饭忙完手中的事就开始午睡,睡了大半个小时,醒来已经是下午四点半了,可是为何完全感受不到饿,难...
    沈小珂书单阅读 238评论 0 0