我的问题的答案在这个问题中得到了暗示,因此我认为答案是禁用我的UIToolbar视图的自动布局。
被认为对视图有效的代码是
cButton.translatesAutoresizingMaskIntoConstraints = YES;
但我不确定它是否适用于我的代码, 因为UIToolbar并没有继承自UIView。
我在游戏中使用了许多小图像,这些图像的大小因设备和方向而异。为了避免有很多不同大小的图像,并在苹果推出新设备时添加新图像,我决定制作一个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];
这是前11版本的外观。工具栏按钮已调整大小并适合底部栏。请注意,我将选中标记的大小减小了50%,只是为了确保我正在查看正确的代码,并且它的行为与我的预期相符。
这是Xcode 9.0 GM和iOS 11模拟器中的外观。请注意,顶部按钮行会正确调整大小,但底部行会扩展以填充分配给选项卡栏的空间。在iPad和其他设备上也会出现相同的行为。
有关如何禁用自动布局或添加约束的任何想法吗?