简体   繁体   中英

iOS Setting navigation item title view frame glitches on iPhone 6 and up

I am using a UIButton for navigation item title view , and the width of the button is subject to change, so I'm setting the frame like below:

CGFloat titleButtonTextWidth = [title sizeWithAttributes:@{NSFontAttributeName:FontMedium(14)}].width;
CGFloat buttonWidth = MAX(titleButtonTextWidth, SCREEN_WIDTH/3.f);

self.titleButton.frame = CGRectMake(0, 0, buttonWidth, 30);

On iPhone 5s and lower, the title button stays at the middle, but for iPhone 6 and up, the origin of the frame is respected and the title button ends up in the top-left corner. Solving this is easy, but I don't know why Apple decided to suddenly change things like this.

Do you guys think this might be a bug or a change?

CGFloat titleButtonTextWidth = [title sizeWithAttributes:@{NSFontAttributeName:FontMedium(14)}].width;
CGFloat buttonWidth = MAX(titleButtonTextWidth, SCREEN_WIDTH/3.f);

self.titleButton.frame = CGRectMake(0, 0, buttonWidth, 30);
self.titleButton.titleLabel.frame = CGRectMake(0, 0, buttonWidth, 30);

change your code to this, this will make title to go in middle of button. You can change your button frame accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM