Add Segmented Control to Navigation Bar and Keep Title with Buttons

Add segmented control to navigation bar and keep title with buttons

I have tried solving your problem using another approach since using a navigation bar only didn't seem to work out (maybe it's because the AppStore app is using a private api but I'm not knowledgeable enough to tell for sure...)
Anyway I simply used a toolbar placed just underneath the navigation bar on which I added a segmented control, all inside a regular UIViewController.

This is what it looks like in Storyboard:
Storyboard

And this is the result in Simulator:

Simulator

Just be careful to offset the table view down to account for the vertical space used up by the toolbar.
Hope this helps!

Trying to handle segmented control switch inside of navigation bar title view

It's because of your UISegmentControl declaration.

You have 2 ways:

1. declare it as a lazy var:

because Self in lazy var is valid.

    private lazy var updateSwitch: UISegmentedControl = {
let sc = UISegmentedControl(items: ["Sign In", "Sign Out"])
sc.anchor(width: 128, height: 32)
sc.selectedSegmentIndex = 0
sc.tintColor = .mainBlue
sc.backgroundColor = .mainGray
sc.addTarget(self, action: #selector(handleSegmentedControlSwitch(_:)), for: .valueChanged)
return sc }()

2. assign action in viewDidLoad

    override func viewDidLoad() {
super.viewDidLoad()
let segmented = self.updateSwitch
segmented.addTarget(self, action: #selector(handleSegmentedControlSwitch(_:)), for: .valueChanged)
navigationItem.titleView = segmented
}

How to add the UISegmentedControl in UINavigationBar?

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....

self.navigationItem.rightBarButtonItem
self.navigationItem.leftBarButtonItems // for adding an Array of items

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.

adding segmented control with in the Navigation Bar

Do you want to use Interface Builder or do it in code only?

With IB operation is very straightforward, you only need to drag segmented control to place on navigation bar where title is located. Title will be replaced by the segmented control.

If you want to accomplish this in code, please refer to this section of iPhone reference library. It seems you need to set navigation item's titleView property to your segmented control, which is subclass of UIView, so this is completely legal.

Why does the selector of my segmented control in a navigation bar not work?

Inside the closure where you're creating mySegmentConstrol, self does not refer to what you think it does. In this case, self refers to something that is of type (ViewController) -> () -> ViewController, which is definitely not what you want:

Some weird type

To refer to the current ViewController instance, declare mySegmentControl as a lazy var:

lazy var mySegmentControl: UISegmentedControl = {

This should get your segmented control selector firing properly.

As for showing your navigation item's title, well... you can't really do that and show a custom view in the navigation bar's titleView. It's one or the other.



Related Topics



Leave a reply



Submit