多个导航项视图 iOS

More than one navigation item view iOS

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

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

您需要隐藏导航栏! 并将导航栏自定义为视图!

[self.navigationController setNavigationBarHidden:YES animated:YES];

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

因为您正在替换第一个视图第二个视图。

尝试使用以下代码

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;