简体   繁体   English

iOS在iPhone 6及更高版本上设置导航项标题视图框架故障

[英]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: 我将UIButton用于navigation item title view ,并且按钮的宽度可能会发生变化,因此我将框架设置如下:

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. 在iPhone 5s及更低版本上,标题按钮位于中间,但在iPhone 6及更高版本上, frameorigin得到尊重,标题按钮最终位于左上角。 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 您可以相应地更改按钮框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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