BarButtonItems and title not appearing in iOS 11

Viewed 1003

Since iOS 11 and Xcode 9 barbuttonitems and titles aren't visible anymore. It doesn't matter whether I'm trying to add a custom view like this:

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)

or just standard UIBarButtonItems like that:

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]

With Xcode 8 everything worked fine.

3 Answers

This issue comes while compiling with Xcode 9,because now UIBarButtonItem is using autolayput as well.Below is the code it to make it work.

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]];

I had the same issue when updating to the xCode9 IDE. I was able to resolve this by using UINavigationBar.appearance:

    let buttonItem = UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self])
    buttonItem.setTitleColor(.black, for: .normal)
    buttonItem.setTitleColor(.gray, for: .disabled)

I had that same issue and none of the above fixed. All I did was to set a width on the titleView and everything worked just fine!

EDIT:

Every UIViewController has a navigationItem property, and every navigationItem has an optional titleView.

For reference: https://developer.apple.com/documentation/uikit/uinavigationitem/1624935-titleview

In my case, I was using a custom titleView and I think that's the cause of the problem, since Apple changed the API to support the new navigation bar layout.

Related