如何在 UINavigationBar 中添加 UISegmentedControl?
How to add the UISegmentedControl in UINavigationBar?
我尝试将 UISegmentedControl
添加到带有标题的 UINavigationBar
的底部。但是我不能添加它,我不能在 tableView.headerView
中添加 UISegmentedControl
,因为我需要 tableView.headerView
中的搜索栏,所以我只需要添加一个解决方案 [=11] =] 到 UINavigationBar
的底部。有人有其他想法可以解决这种情况吗?
这是我试过的代码(Swift):
class searchingViewController: UITableViewController{
override func viewDidLoad() {
var items: [AnyObject] = ["Searching Method A", "Searching Method B"]
var searchSC:UISegmentedControl!
searchSC.frame = CGRectMake(0, 0, frame.width - 20, 30)
searchSC.selectedSegmentIndex = 1
searchSC.backgroundColor = UIColor(white: 1, alpha: 1)
searchSC.layer.cornerRadius = 5.0
navigateUIToolBar = UIToolbar(frame: CGRectMake(frame.minX + 10, ios7_8Info.getStatusBarHeight()+self.navigationController!.navigationBar.frame.height+frame.minY+(21/2),
frame.width - 20, 30))
navigateUIToolBar.addSubview(searchSC)
self.navigationController?.navigationBar.addSubview(navigateUIToolBar)
}
}
将它放在导航栏中有这样的东西的右左和标题视图...访问使用....
self.navigationItem.titleView = mySegmentedControl
供将来参考....
self.navigationItem.rightBarButtonItem
self.navigationItem.leftBarButtonItems // for adding an Array of items
要将其添加到导航视图下方但保持静态..将其添加到 viewController.view...您使用的是 UITableViewController 吗?如果是这样,可以切换到 UIViewController 并添加一个 tableView,然后将您的工具栏视图作为子视图添加到 self.view。
你可以自定义navigationItem的titleView,像这样:
self.navigationItem.titleView = segmentview
你不应该在导航栏上添加子视图,因为在 push 或 pop UIViewcontroller 之后,子视图仍然存在于导航栏上。
这是我在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(其中一个 segmentViewController)中:
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)];
在Swift5
中添加UINavigationBar中的段控件
let segment: UISegmentedControl = UISegmentedControl(items: ["First", "Second"])
segment.sizeToFit()
if #available(iOS 13.0, *) {
segment.selectedSegmentTintColor = UIColor.red
} else {
segment.tintColor = UIColor.red
}
segment.selectedSegmentIndex = 0
segment.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "ProximaNova-Light", size: 15)!], for: .normal)
self.navigationItem.titleView = segment
我想你正在找这个。
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
您还可以将分段控件添加到搜索栏,而不是导航项。如果你像我一样,我需要一个大标题+搜索栏+导航项,这对于当前的最佳答案来说是不可能的。
@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
的那一行是您问题的解决方案。
我尝试将 UISegmentedControl
添加到带有标题的 UINavigationBar
的底部。但是我不能添加它,我不能在 tableView.headerView
中添加 UISegmentedControl
,因为我需要 tableView.headerView
中的搜索栏,所以我只需要添加一个解决方案 [=11] =] 到 UINavigationBar
的底部。有人有其他想法可以解决这种情况吗?
这是我试过的代码(Swift):
class searchingViewController: UITableViewController{
override func viewDidLoad() {
var items: [AnyObject] = ["Searching Method A", "Searching Method B"]
var searchSC:UISegmentedControl!
searchSC.frame = CGRectMake(0, 0, frame.width - 20, 30)
searchSC.selectedSegmentIndex = 1
searchSC.backgroundColor = UIColor(white: 1, alpha: 1)
searchSC.layer.cornerRadius = 5.0
navigateUIToolBar = UIToolbar(frame: CGRectMake(frame.minX + 10, ios7_8Info.getStatusBarHeight()+self.navigationController!.navigationBar.frame.height+frame.minY+(21/2),
frame.width - 20, 30))
navigateUIToolBar.addSubview(searchSC)
self.navigationController?.navigationBar.addSubview(navigateUIToolBar)
}
}
将它放在导航栏中有这样的东西的右左和标题视图...访问使用....
self.navigationItem.titleView = mySegmentedControl
供将来参考....
self.navigationItem.rightBarButtonItem
self.navigationItem.leftBarButtonItems // for adding an Array of items
要将其添加到导航视图下方但保持静态..将其添加到 viewController.view...您使用的是 UITableViewController 吗?如果是这样,可以切换到 UIViewController 并添加一个 tableView,然后将您的工具栏视图作为子视图添加到 self.view。
你可以自定义navigationItem的titleView,像这样:
self.navigationItem.titleView = segmentview
你不应该在导航栏上添加子视图,因为在 push 或 pop UIViewcontroller 之后,子视图仍然存在于导航栏上。
这是我在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(其中一个 segmentViewController)中:
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)];
在Swift5
中添加UINavigationBar中的段控件 let segment: UISegmentedControl = UISegmentedControl(items: ["First", "Second"])
segment.sizeToFit()
if #available(iOS 13.0, *) {
segment.selectedSegmentTintColor = UIColor.red
} else {
segment.tintColor = UIColor.red
}
segment.selectedSegmentIndex = 0
segment.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "ProximaNova-Light", size: 15)!], for: .normal)
self.navigationItem.titleView = segment
我想你正在找这个。
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
您还可以将分段控件添加到搜索栏,而不是导航项。如果你像我一样,我需要一个大标题+搜索栏+导航项,这对于当前的最佳答案来说是不可能的。
@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
的那一行是您问题的解决方案。