我在这个问题中暗示了我的问题的答案,所以我认为答案是禁用我的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 = …Run Code Online (Sandbox Code Playgroud) 最近在我们的项目中,使用customView的UIBarButtonItem出现了问题.在iOS 11之前,我们通过灵活的间距项目进行了布局.这不再起作用,因此没有显示任何内容.
因为我没有在SO上找到真正为我解决问题的答案,所以我调查了它,想出了一个我想与你分享的(当然是一种hacky)解决方案.
也许它可以帮助你或你有一些反馈.这是混合的objc和swift代码,希望你不要介意.