我一直在尝试使用iOS11中UIBarButton的contentEdgeInsets将UIBarButton-> UIButton更改为-20。但是没有任何效果。我还尝试添加灵活的空间,但是仍然无法正常工作。下面是我的代码

    let btn1 = UIButton(type: .custom)
    let imageString = UserDefaults.standard.object(forKey: "UserImage") as! String
    let imageURl = URL(string: imageString)
    btn1.sd_setBackgroundImage(with: imageURl, for: .normal, placeholderImage: UIImage(named: "profilePlaceHolder.png"))
    btn1.imageView?.contentMode = .scaleAspectFit
    btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    btn1.applyNavBarConstraints(size: (width: 30, height: 30))
    //btn1.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0)
    btn1.layer.cornerRadius = 15
    btn1.layer.borderColor = UIColor.appColorPurple.cgColor
    btn1.layer.borderWidth = 1
    btn1.layer.masksToBounds = true
    btn1.addTarget(self, action: #selector(BaseViewController.showProfile), for: .touchUpInside)
    let item1 = UIBarButtonItem(customView: btn1)
    self.navigationItem.leftBarButtonItems = [item1]

    func applyNavBarConstraints(size: (width: CGFloat, height: CGFloat)) {
        let widthConstraint = self.widthAnchor.constraint(equalToConstant: size.width)
        let heightConstraint = self.heightAnchor.constraint(equalToConstant: size.height)
        heightConstraint.isActive = true
        widthConstraint.isActive = true
    }


检查此图像我想要什么。

ios - 自动布局应用于UIButton时如何移动UIBarButton?-LMLPHP

最佳答案

您必须像这样使用:

btn1.leftAnchor.constraint(equalTo: btn1.leftAnchor, constant: -20).isActive = true
let item1 = UIBarButtonItem(customView: btn1)
self.navigationItem.leftBarButtonItems = [item1]

关于ios - 自动布局应用于UIButton时如何移动UIBarButton?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47062491/

10-12 04:24