gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:02:16 25 4
gpt4 key购买 nike

我一直在尝试使用 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
}

检查这张图片是我想要的。

enter image description here

最佳答案

你必须这样使用:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com