James
James

Reputation: 762

Resize bar button item in iOS 11 without using auto layout

I have an old app that I wrote as a beginner that I'd like to update for iOS 11. The app is written in Objective-C and does not use auto layout.

This is how the bar button items in the navigation bar should look (and how they do look in iOS 10):

iOS 10

However, in iOS 11 the buttons look like this:

enter image description here


How do I resize the buttons so they look like they did in iOS 10 without using auto layout? (My project didn't use auto layout back then.) Thanks in advance.

Upvotes: 0

Views: 658

Answers (1)

richiereitz
richiereitz

Reputation: 163

As of iOS 11, if you want a UIBarButtonItem with a custom size, you're going to have to set its widthAnchor and heightAnchor properties. If you were manually setting the frame before, make sure you still do that so your buttons don't disappear.

Your code might look something like:

/* setting 1) frame, 2) widthAnchor, and 3) heightAnchor because iOS 11 
needs the latter two, and lower versions need the frame */

/* 1) */
[self.nextButton setFrame:(CGRect){
    .size = nextButton_size
}];

/* 2) */
[self.nextButton.widthAnchor constraintEqualToConstant:nextButton_size.width].active = YES;

/* 3) */
[self.nextButton.heightAnchor constraintEqualToConstant:nextButton_size.height].active = YES;

UIBarButtonItem* const nextBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.nextButton];
[self.navigationItem setRightBarButtonItem:nextBarButton];

Upvotes: 1

Related Questions