多个导航项目查看iOS

时间:2015-10-17 12:08:39

标签: ios uinavigationcontroller uisegmentedcontrol uisearchcontroller

我试图将搜索栏和分段控件放入导航栏。目前这是我的代码,它可以工作,但只能添加其中一个。

self.navigationItem.titleView = searchController.searchBar; self.navigationItem.titleView = segmentedControl1;

2 个答案:

答案 0 :(得分:1)

因为您要替换第一视图第二视图。

尝试使用以下代码

CGRect titleViewFrame = self.navigationController.navigationBar.frame;

CGRect searchBarFrame = titleViewFrame;
searchBarFrame.size.width = titleViewFrame.size.width/2;//say, the search bar width = 0.5*search_bar_width

CGRect segmentFrame = titleViewFrame;
segmentFrame.origin.x = searchBarFrame.size.width;
segmentFrame.size.width = searchBarFrame.size.width;

UIView *titleView = [[UIView alloc]initWithFrame:titleViewFrame];
searchController.searchBar.frame = searchBarFrame;//set here changed search bar frame
[titleView addSubview:searchController.searchBar];


UIView *segmentView = [[UIView alloc]initWithFrame:segmentFrame];
segmentView.backgroundColor =[UIColor redColor];

[titleView addSubview:segmentView];

self.navigationItem.titleView = titleView; 

答案 1 :(得分:0)

你需要告诉你的导航栏! 并自定义导航栏作为视图!

iterator

使用UIView自定义该导航控制器的视图。

相关问题