【发布时间】:2017-09-15 21:04:16
【问题描述】:
this question 暗示了我的问题的答案,所以我认为答案是为我的 UIToolbar 视图禁用自动布局。
据说适用于视图的代码是
cButton.translatesAutoresizingMaskIntoConstraints = YES;
但我不确定它是否适用于我的代码,因为 UIToolbar 不继承自 UIView。
我在游戏中使用了许多不同大小的小图像,具体取决于设备和方向。在 Apple 推出新设备时,我决定制作一张 160x160 的图像,然后在使用时调整大小,而不是拥有许多不同的图像,并在 Apple 推出新设备时添加新图像。这在 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];
这就是 pre11 的样子。栏按钮项目已调整大小并很好地适合底部栏。请注意,我将复选标记的大小减少了 50%,只是为了确保我查看的是正确的代码并且它的行为符合我的预期。
这是它们在 Xcode 9.0 GM 和 iOS 11 模拟器中的样子。请注意,顶行的按钮会正确调整大小,但底行会扩展以填充为标签栏分配的空间。同样的行为也发生在 iPad 和各种设备上。
关于如何禁用自动布局或添加约束的任何想法?
【问题讨论】: