我尝试将UISegmentedControl
添加到UINavigationBar
的底部并添加标题。但是我无法添加它,我无法在UISegmentedControl
中添加tableView.headerView
,因为我需要tableView.headerView
中的搜索栏,因此我只需添加一个解决方案{{1 } {到} UISegmentedControl
的底部。任何人都有另一种想法,我可以解决这种情况吗?
以下是我尝试过的代码(使用 Swift ):
UINavigationBar
答案 0 :(得分:18)
要把它放在navigationBar中,左边有一个标题视图,这些东西......用....访问
self.navigationItem.titleView = mySegmentedControl
供将来参考....
self.navigationItem.rightBarButtonItem
self.navigationItem.leftBarButtonItems // for adding an Array of items
要将其添加到导航视图下方,但将其设置为静态..将其添加到viewController.view ...您使用的是UITableViewController吗?如果是这样,可以切换到UIViewController并添加一个tableView,然后将您的toolbarview作为该self.view的子视图。
答案 1 :(得分:18)
在Swift 5中的UINavigationBar中添加段控件
let segment: UISegmentedControl = UISegmentedControl(items: ["First", "Second"])
segment.sizeToFit()
segment.tintColor = UIColor(red:0.99, green:0.00, blue:0.25, alpha:1.00)
segment.selectedSegmentIndex = 0;
segment.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "ProximaNova-Light", size: 15)!], for: .normal)
self.navigationItem.titleView = segment
答案 2 :(得分:5)
我认为你正在寻找这个。
let searchVC = self.storyboard?.instantiateViewController(withIdentifier:"idofcontroller")
let searchController = UISearchController(searchResultsController: searchVC)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["News", "Photos", "Videos"]
searchController.searchBar.delegate = self
答案 3 :(得分:3)
这就是我在Objective C中的表现(对不起,我还没有学过Swift):
- (void)viewDidLoad
{
[super viewDidLoad];
self.viewControllers = [self segmentViewControllers];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems: [self.segmentedControl addTarget: self
action: @selector(segmentClicked:)
forControlEvents: UIControlEventValueChanged];
CGFloat topOffset = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
self.toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, topOffset, self.navigationController.navigationBar.frame.size.width, kDefaultViewHeight)];
self.toolbar.delegate = self;
self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
self.toolbar.backgroundColor = [UIColor whiteColor];
self.toolbar.clipsToBounds = YES;
UIBarButtonItem *segmentedControlItem = [[UIBarButtonItem alloc] initWithCustomView: self.segmentedControl];
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target: nil
action: nil];
[self.toolbar setItems: @[flexibleItem, segmentedControlItem, flexibleItem] animated: YES];
[self.navigationController.view addSubview: self.toolbar];
self.segmentedControl.selectedSegmentIndex = 0;
}
在UITableViewController(segmentViewControllers之一)中:
self.tableView.contentInset = UIEdgeInsetsMake(topOffset, 0, 0, 0);
CGRect currentFrame = self.tableView.frame;
[self.tableView setFrame: CGRectMake(currentFrame.origin.x,
currentFrame.origin.y,
currentFrame.size.width,
currentFrame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height)];
答案 4 :(得分:1)
你可以自定义navigationItem的titleView,如下所示:
self.navigationItem.titleView = segmentview
你不应该将子视图添加到导航栏,对于推后或弹出UIViewcontroller,子视图仍然在导航栏上显示。
答案 5 :(得分:1)
您还可以将细分控件添加到搜索栏,而不是导航项。如果您像我一样,我需要一个大的标题+搜索栏+导航项,这对于当前的最佳答案是不可能的。
@Abhishek的答案很接近。您将执行以下操作:
// Create the search controller
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.scopeButtonTitles = ["Option 1", "Option 2"]
// Make sure the scope bar is always showing, even when not actively searching
searchController.searchBar.showsScopeBar = true
// Make sure the search bar is showing, even when scrolling
navigationItem.hidesSearchBarWhenScrolling = false
// Add the search controller to the nav item
navigationItem.searchController = searchController
definesPresentationContext = true
@Gurjit Singh,带有.showsScopeBar = true
的行是您的问题的解决方案。