由于iOS 11和Xcode 9 barbuttonitems和标题不再可见。我是否正在尝试添加这样的自定义视图并不重要:
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一切正常。
答案 0 :(得分:1)
使用Xcode 9进行编译时出现此问题,因为现在UIBarButtonItem
也在使用autolayput .Below是使其工作的代码。
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]];
答案 1 :(得分:0)
更新到xCode9
IDE时遇到了同样的问题。我可以使用UINavigationBar.appearance
:
let buttonItem = UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self])
buttonItem.setTitleColor(.black, for: .normal)
buttonItem.setTitleColor(.gray, for: .disabled)
答案 2 :(得分:0)
我有同样的问题而且以上都没有修复。
我所做的就是在width
上设置一个titleView
,一切正常!
编辑:
每个UIViewController
都有一个navigationItem
属性,每个navigationItem
都有一个可选的titleView
。
供参考:https://developer.apple.com/documentation/uikit/uinavigationitem/1624935-titleview
在我的情况下,我使用自定义titleView
,我认为这是问题的原因,因为Apple更改了API以支持新的导航栏布局。