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