将分段控件添加到视图控制器中的导航栏

时间:2017-10-17 06:07:25

标签: ios swift swift3 uinavigationbar uisegmentedcontrol

我有一个顶部有导航栏的UIViewController。我想用分段控件替换标题。

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);
    }

2 个答案:

答案 0 :(得分:0)

好发现问题。

您错过的是将titleView设置为您创建的导航项。

title.titleView = segment

您正在将titleView添加到导航项self.navigationItem.titleView = segment;,但您需要将其设置为您传递到导航栏的navigationItem

最终代码

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);
}

修改

如何设置导航栏框架还有另一个问题。您的代码的O / P看起来像

enter image description here

我相信那不是你想要的!之所以看起来是因为你的框架从y = 0开始,但在iPhone和iPad的移植模式中,顶部有一个状态栏,根据各种条件消耗不同的空间量(通常人们硬编码或假设它是20但iPhone X的高度状态栏可能会有所不同,甚至在正常的iPhone中,当您收到呼叫状态栏高度变化时)。最好使用自动布局和安全边距来向View添加导航栏。

但如果您真的想要硬编码帧,那么您可以使用

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

O / P看起来像

enter image description here

希望它有所帮助。

答案 1 :(得分:0)

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

只是在操作中分配对象的顺序。