gpt4 book ai didi

ios - 在 Swift4 中,替换 navigationItem.leftBarButtonItem 会调整不需要的大小

转载 作者:行者123 更新时间:2023-11-28 21:05:05 34 4
gpt4 key购买 nike

返回到具有不同帧和图像值的 VC 时,UIBarButtonItem 调整大小有问题。

var selectedR = 1
var leftFrame = CGRect()
var leftImage = UIImage()

override func viewDidLoad() {
super.viewDidLoad()

if selectedR == 0
{
leftFrame = CGRect(x: 0, y: 0, width: 33, height: 33)
leftImage = UIImage(named: “pic-0”)!
}
else if selectedR == 1
{
leftFrame = CGRect(x: 0, y: 0, width: 79, height: 33)
leftImage = UIImage(named: "pic-1")!
}
else if selectedR == 2
{
leftFrame = CGRect(x: 0, y: 0, width: 34, height: 33)
leftImage = UIImage(named: "pic-2”)!
}

let cButton = UIButton(frame: leftFrame)
cButton.setImage(leftImage, for: UIControlState())
cButton.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
cButton.contentMode = UIViewContentMode.scaleAspectFit
let leftItem = UIBarButtonItem(customView: cButton)
self.navigationItem.leftBarButtonItem = leftItem
}

selectedR = 1 的第一次完美尺寸

使用不同的 selectedR 值返回到 viewDidLoad 时,图像会爆炸、扭曲并且设置左帧大小不被接受。

这一切在 swift 3 中都完美无缺,但在 swift 4 中却无处不在。任何帮助或建议将不胜感激。

最佳答案

iOS 10 和 11 之间的一个主要变化是现在栏按钮项目由自动布局引擎布局。通常这应该是向后兼容的。但是,如果您在布局栏按钮项目时遇到问题,您应该尝试设置约束,看看是否能解决您的问题。

在你的情况下你会说:

var selectedR = 1
var leftFrame = CGRect()
var leftImage = UIImage()

override func viewDidLoad() {
super.viewDidLoad()

if selectedR == 0
{
leftFrame = CGRect(x: 0, y: 0, width: 33, height: 33)
leftImage = UIImage(named: “pic-0”)!
}
else if selectedR == 1
{
leftFrame = CGRect(x: 0, y: 0, width: 79, height: 33)
leftImage = UIImage(named: "pic-1")!
}
else if selectedR == 2
{
leftFrame = CGRect(x: 0, y: 0, width: 34, height: 33)
leftImage = UIImage(named: "pic-2”)!
}

let cButton = UIButton(frame: leftFrame)
cButton.widthAnchor.constraint(equalToConstant: leftFrame.width).isActive = true
cButton.heightAnchor.constraint(equalToConstant: leftFrame.height).isActive = true
cButton.setImage(leftImage, for: UIControlState())
cButton.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
cButton.contentMode = UIViewContentMode.scaleAspectFit
let leftItem = UIBarButtonItem(customView: cButton)
self.navigationItem.leftBarButtonItem = leftItem
}

Apple 的 WWDC 2017 session 对此进行了介绍 Updating Your App for iOS 11 .

关于ios - 在 Swift4 中,替换 navigationItem.leftBarButtonItem 会调整不需要的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46203703/

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