gpt4 book ai didi

ios - 多个导航项 View iOS

转载 作者:行者123 更新时间:2023-11-29 01:33:44 30 4
gpt4 key购买 nike

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

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

最佳答案

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

试试下面的代码

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;

关于ios - 多个导航项 View iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33186451/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com