gpt4 book ai didi

uibarbuttonitem - BarButtonItems 和标题未出现在 iOS 11 中

转载 作者:行者123 更新时间:2023-12-04 07:02:11 24 4
gpt4 key购买 nike

自 iOS 11 和 Xcode 9 以来,栏按钮项和标题不再可见。我是否尝试添加这样的自定义 View 并不重要:

let backButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: 180, height: 32))
backButton.setImage(UIImage(named: "back_icon")?.withRenderingMode(.alwaysTemplate), for: .normal)
backButton.tintColor = UIColor.white
backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
backButton.backgroundColor = UIColor.clear
backButton.titleLabel?.font = UIFont(name: SWMainHelper.sharedInstance.mediumFont, size: 18)
backButton.setTitleColor(UIColor.white, for: .normal)
backButton.setTitle("Go back", for: .normal)

backButton.sizeToFit()

backButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0)

backButton.frame.size.width += 16

let negativeButtonSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeButtonSpace.width = -16

self.navigationItem.setLeftBarButtonItems([negativeButtonSpace, UIBarButtonItem(customView: backButton)], animated: true)

或者像这样的标准 UIBarButtonItems:

let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
add.tintColor = UIColor.white
let play = UIBarButtonItem(title: "Play", style: .plain, target: self, action: #selector(playTapped))
play.tintColor = UIColor.white
navigationItem.rightBarButtonItems = [add, play]

使用 Xcode 8 一切正常。

最佳答案

这个问题是在使用 Xcode 9 编译时出现的,因为现在 UIBarButtonItem 也在使用 autolayput。下面是让它工作的代码。

UIButton *leftCustomButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 100)];

[leftCustomButton.widthAnchor constraintEqualToConstant:100].active = YES;
[leftCustomButton.heightAnchor constraintEqualToConstant:35].active = YES;
[leftCustomButton setTitle:@"TEST" forState:UIControlStateNormal];
[leftCustomButton.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
[leftCustomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIBarButtonItem * leftButtonItem =[[UIBarButtonItem alloc] initWithCustomView:leftCustomButton];
[self.navigationItem setRightBarButtonItems:@[leftButtonItem]];

关于uibarbuttonitem - BarButtonItems 和标题未出现在 iOS 11 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46402506/

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