How to adjust UIToolBar left and right padding

后端 未结 10 538
自闭症患者
自闭症患者 2020-12-02 05:05

I create one UIToolbar with code and another with interface builder. But, found out the two toolbar having different left and right padding which shown below:

From I

10条回答
  •  借酒劲吻你
    2020-12-02 05:44

    This is a great question and even with the solutions provided - it did not resolve the problem on iOS11.

    WORKING SOLUTION

    If you create a new extension of UIButton:

    class BarButton: UIButton {
    
        override var alignmentRectInsets: UIEdgeInsets {
            return UIEdgeInsetsMake(0, 16, 0, 16)
        }
    
    }
    

    Then when you use it in your layout:

    lazy var btnLogin: BarButton = {
        let btn = BarButton(type: .custom)
        // Add your button settings
    
        btn.widthAnchor.constraint(equalToConstant: self.frame.size.width).isActive = true
        btn.heightAnchor.constraint(equalToConstant: 50).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()
    

    This solved a very annoying problem, if you have any issues give me a shout.

提交回复
热议问题