I have tried to add the UISegmentedControl
to the bottom of UINavigationBar
with title. But i cannot add it and I cannot add UISegmentedControl
in the tableView.headerView
,because i need the search bar in the tableView.headerView
, so there is just one solution that i need to add UISegmentedControl
to the bottom of UINavigationBar
. Anyone have another idea that i can solve this situation?
Here is the code that I have tried(with 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)
}
}
I think you are looking for this.
This is how I do it in Objective C (sorry, I haven't learned Swift yet):
And in UITableViewController (one of the segmentViewControllers):
To put it in the navigationBar has a right left and title view for such things... accessed using....
self.navigationItem.titleView = mySegmentedControl
for future reference....
To add it below the navigation view but have it static.. add it to the viewController.view... Are you using a UITableViewController? If so maybe switch to a UIViewController and add a tableView then your toolbarview as subviews to that self.view.
To add segment control in UINavigationBar in Swift 4
You can also add the segment control to a search bar, not the navigation item. If you're like me, I needed a large title + search bar + navigation item, which would be impossible with the current top answer.
@Abhishek 's answer is close. You would do something like the following:
@Gurjit Singh, the line with
.showsScopeBar = true
is the solution to your problem.you can custom navigationItem's titleView, like this:
you should not add subview to the navigationbar, for after push or pop UIViewcontroller ,the subview is still exsit on the navigationbar.