简体   繁体   中英

Adding a segmentedcontrol to a navigationbar in a view controller

I have a UIViewController with a navigation bar on top. I want to replace the title with a segmented control.

class AViewController: UIViewController
{
    private var navigationbar = UINavigationBar();
    private var segment = UISegmentedControl();

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        buildThebar();
    }

    func buildThebar()
    {
        navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 64));

        navigationbar.backgroundColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0);

        segment = UISegmentedControl(items: ["Testy", "Tests"]);
        segment.sizeToFit();
        segment.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0);

        let cancel = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelbar(sender:)));

        cancel.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0);

        let title = UINavigationItem(title: "Testing");

        title.leftBarButtonItem = cancel;

        navigationbar.setItems([title], animated: false);

        self.navigationItem.titleView = segment;
        // self.navigationItem.leftBarButtonItem = cancel;

        self.view.addSubview(navigationbar);
    }

Ok found issue.

What you missed is setting titleView to the navigation item you created.

title.titleView = segment

You are adding titleView to navigation item self.navigationItem.titleView = segment; but you need to set it to your navigationItem which you are passing to your navigation bar.

Final code

func buildThebar()
{

    navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 64));

    navigationbar.backgroundColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0);

    segment = UISegmentedControl(items: ["Testy", "Tests"]);
    segment.sizeToFit();
    segment.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0);

    let cancel = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: nil);

    cancel.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0);

    let title = UINavigationItem(title: "Testing"); //better let title = UINavigationItem()
    title.leftBarButtonItem = cancel;
    title.titleView = segment
    navigationbar.setItems([title], animated: false);

    // self.navigationItem.leftBarButtonItem = cancel;

    self.view.addSubview(navigationbar);
}

EDIT:

There is another issue with how you are setting navigation bar frame. O/P with your code looks like

在此处输入图片说明

I believe thats not what you want! the reason why it looks like that is because your frame starts from y = 0 but in iPhone and iPads portait mode there is a status bar on top which consumes variable amount of space based on various condition (Usually people hardcode or assume it to be 20 but iPhone X the height status bar might vary and even in normal iPhones when you receive a call status bar height changes). Its always better to use auto layout and safe margin to add navigation bar to View.

But if you really wanna hard code frames then you can use

navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 44));

O/P looks like

在此处输入图片说明

Hope it helps.

    let title = UINavigationItem()
        title.titleView = segment
        title.rightBarButtonItem = cancel
navigationbar.setItems([title], animated: false);
self.view.addSubview(navigationbar);

Just a matter of order of assignment of the objects in operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM