美文网首页
在 navigation 添加 居中 的 segmentCont

在 navigation 添加 居中 的 segmentCont

作者: innepeace | 来源:发表于2016-12-08 18:03 被阅读0次

如题,在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 。

相关文章

网友评论

      本文标题:在 navigation 添加 居中 的 segmentCont

      本文链接:https://www.haomeiwen.com/subject/icmsmttx.html