iOS 11 UIBarButtonItem images not sizing

后端 未结 2 1702
暗喜
暗喜 2020-12-03 14:30

The answer to my question was hinted at in this question, so I think the answer is to disable autolayout for my UIToolbar view.

The code that is said to work for vi

相关标签:
2条回答
  • 2020-12-03 15:07

    Fallstreak answer (+1) was the solution in my case. I was having a custom view not work on tap. Just wanted to share what I had to do for a custom view in a navigation item to work. This is all swift 3

    let backButtonView = BackButtonView.loadNib()
    backButtonView?.addTarget(self, action: #selector(returnToPrevious), for: .touchUpInside)
    backButtonView.widthAnchor.constraint(equalToConstant: backButtonView.frame.width).isActive = true
    backButtonView.heightAnchor.constraint(equalToConstant: backButtonView.frame.height).isActive = true
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButtonView!)
    

    The 2 lines about the anchor width/height came from Fallstreak's answer and were the solution in this case.

    0 讨论(0)
  • 2020-12-03 15:15

    BarButtonItem (iOS11\xCode9) uses autolayout instead of frames. Try this (Swift):

    if #available(iOS 9.0, *) {
        cButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
        cButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true
    }
    

    Objective C

    if (@available(iOS 9, *)) {
         [cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
         [cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
    }
    
    0 讨论(0)
提交回复
热议问题