不正确的垂直导航项对齐

时间:2016-08-23 10:36:18

标签: swift uinavigationbar vertical-alignment

我尝试自定义导航栏并使用垂直对齐方式将其设置得更大。这是我的代码:

   @IBOutlet weak var topNavTitleLabel: UILabel!
    @IBOutlet weak var topNavStackView: UIStackView!
    @IBOutlet weak var leftNavHomeButton: UIBarButtonItem!
    @IBOutlet weak var rightNavSearchButton: UIBarButtonItem!

    var rightNavSearchButton_: UIBarButtonItem?

    @IBAction func searchAction(sender: UIButton) {

        topNavStackView.hidden = true
        topNavStackView.hidden = false
        rightNavSearchButton_ = rightNavSearchButton
        navigationItem.rightBarButtonItem = nil
    }

    @IBAction func performSearch(sender: UIButton) {
        topNavStackView.hidden = false
        topNavStackView.hidden = true
        navigationItem.rightBarButtonItem =  rightNavSearchButton_
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if let navConBar = self.navigationController?.navigationBar {
            navConBar.bounds = CGRectMake(0, 0, view.bounds.width, 100)
            navConBar.setTitleVerticalPositionAdjustment(-10.0, forBarMetrics: .Default)
            let offset = UIOffset(horizontal: 0, vertical: -10)
            navigationItem.rightBarButtonItem?.setTitlePositionAdjustment(offset, forBarMetrics: .Default)
            navigationItem.leftBarButtonItem?.setTitlePositionAdjustment(offset, forBarMetrics: .Default)
        }
    }

当它出现时看起来不错。

alignment is ok

但是当我点击右侧导航栏按钮时,左按钮会变为向下。

alignment isn't ok

当它回头时:

alignment isn't ok

如何解决?

0 个答案:

没有答案