如何在UINavigationBar中添加UISegmentedControl?
我试图将UISegmentedControl
添加到带有标题的UINavigationBar
的底部。 但我不能添加它,我不能添加UISegmentedControl
在tableView.headerView
,因为我需要在tableView.headerView
search栏,所以只有一个解决scheme,我需要将UISegmentedControl
添加到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然后你的toolbarview作为子视图self.view。
在Swift中的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([NSFontAttributeName: UIFont(name:"ProximaNova-Light", size: 15)!], forState: UIControlState.Normal) self.navigationItem.titleView = segment
这是我如何在目标C(对不起,我还没有学到斯威夫特):
- (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)];
你可以自定义navigationItem的titleView,就像这样:
self.navigationItem.titleView = segmentview
你不应该将子视图添加到导航栏,在推或者popupUIViewcontroller之后,子视图仍然存在于导航栏上。
我想你正在寻找这个。
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