iOS 11 UIBarButtonItem图像没有大小调整

JSc*_*rry 14 uitabbar ios ios11

我在这个问题中暗示了我的问题的答案,所以我认为答案是禁用我的UIToolbar视图的自动布局.

据说适用于视图的代码是

cButton.translatesAutoresizingMaskIntoConstraints = YES;
Run Code Online (Sandbox Code Playgroud)

但我不确定它是否适用于我的代码,因为UIToolbar不会从UIView继承.

我有很多小图像,我在游戏中使用不同大小,具体取决于设备和方向.当Apple推出新设备时,我决定先制作一张160x160的图像,然后在使用时调整大小,而不是拥有大量不同的图像,并添加新图像.这在iOS 4 - iOS 10中运行良好,但在iOS 11中失败.

代码非常简单:

// Get the image
NSString *pictFile = [[NSBundle mainBundle] pathForResource:@"Correct" ofType:@"png"];
UIImage *imageToDisplay = [UIImage imageWithContentsOfFile:pictFile];
UIImage *cImage  = [UIImage imageWithCGImage:imageToDisplay.CGImage scale:[UIScreen mainScreen].scale orientation:imageToDisplay.imageOrientation];

UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cButton setImage:cImage forState:UIControlStateNormal];
[cButton setTitle:@"c" forState:UIControlStateNormal];

//set the frame of the button to the size of the image
cButton.frame = CGRectMake(0, 0, standardButtonSize.width, standardButtonSize.height);

//create a UIBarButtonItem with the button as a custom view
c = [[UIBarButtonItem alloc] initWithCustomView:cButton];
Run Code Online (Sandbox Code Playgroud)

这就像pre11.条形按钮项目已调整大小并适合底部栏.注意我将复选标记的大小减小了50%,以确保我正在查看正确的代码并且它的行为与我期望的一样.

正确的版本

这是他们在Xcode 9.0 GM和iOS 11的模拟器中的样子.请注意,顶行按钮正确调整大小,但底行扩展以填充为标签栏分配的空间.同样的行为也发生在iPad以及各种设备上.

iOS 11

有关如何禁用Autolayout或添加约束的任何想法?

小智 48

BarButtonItem(iOS11\xCode9)使用自动布局而不是帧.试试这个(斯威夫特):

if #available(iOS 9.0, *) {
    cButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
    cButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true
}
Run Code Online (Sandbox Code Playgroud)

目标C.

if (@available(iOS 9, *)) {
     [cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
     [cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
}
Run Code Online (Sandbox Code Playgroud)