无法将barButtonItem放置在navigationBar的下角

时间:2019-02-12 15:38:24

标签: ios swift autolayout constraints

当前无法更改barButtonItem的高度。我添加了一个自定义视图,还使用了一个显示标题放大的navBar。我希望即使显示带有大标题的navBar时navBarButton项也可以选择,并更新barButtonItem的位置,使其与标题相邻。 barButtonItem不能放置在navBar的下角,并且barButtonItem的目标不会记录任何点击...

出于某种奇怪的原因,我只能调整customView的宽度,但高度约束与另一个约束(可能在stackView中)冲突。

我已经更改了视图的框架,并且在拖动navBar时,customView的位置确实发生了变化,但是由于可点击区域仅限于导航条的特定部分,因此它无法注册任何操作。真的很困惑

我尝试将一个自定义视图添加到rightBarButtonItem,但最终只是根据navBar的大小更改视图的框架以进行更新。

func setupBarItem() {    
    guard let navBar = self.navigationController?.navigationBar else {return}
    let image = UIImage(named: "large_shopping_cart")
    let imageView = UIImageView(image: image)


    imageView.frame.origin.x = 0
    imageView.frame.origin.y = 0
    imageView.frame.size.height = 40
    imageView.frame.size.width = 40
    imageView.frame.inset(by: UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0))
    imageView.backgroundColor = .yellow

    self.imageView = imageView

    self.imageView?.widthAnchor.constraint(equalToConstant: 40).isActive = true
    self.imageView?.heightAnchor.constraint(equalToConstant: 40).isActive = true

    let checkButton = UIBarButtonItem(customView: self.imageView!)

    let otherButton = UIBarButtonItem(image: #imageLiteral(resourceName: "shopping_cart_icon"), landscapeImagePhone: nil, style: .plain, target: self, action: #selector(tappedCheckout(_:)))

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tappedCheckout(_:)))
    print("This is the navbar height when the view loads \(navBar.bounds.height)")

    checkoutBarButtonItem = checkButton

    self.navigationItem.rightBarButtonItem = checkoutBarButtonItem
    self.navigationItem.rightBarButtonItem?.customView?.addGestureRecognizer(tapGestureRecognizer)

    checkoutBarButtonItem?.customView?.layoutIfNeeded()
    navBar.layoutIfNeeded()

}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard let navBar = self.navigationController?.navigationBar else {return}

//        print("\(navBar.bounds.height)")

    if navBar.bounds.height > 90 {

        imageView?.frame.origin.x = 0
        imageView?.frame.origin.y = navBar.bounds.height - 50

        imageView?.image = #imageLiteral(resourceName: "large_shopping_cart")

        imageView?.frame.size.height = 40
        imageView?.frame.size.width = 40

    } else {
        imageView?.frame.origin.x = -10
        imageView?.frame.origin.y = 0

        imageView?.frame.size.height = 30
        imageView?.frame.size.width = 30

    }
}
}

0 个答案:

没有答案