我有一个 UIView,其中包含一个 UIToolBar,其中有两个 UIButtons 和一个灵活的空间。由于某些奇怪的原因,在 iPhone 6 和 6Plus 上,但在任何其他型号的 iPhone 上都没有,UIToolBar 中的左右 UIButton 似乎显示一半在屏幕上,一半在屏幕外。
这是我遇到的 iPhone 6 / 6Plus 问题的示例
这是它在所有其他 i 设备上的外观
这是我用来创建 UIToolBar、按钮和标签的代码。
UILabel *toolBarLabel = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth - 150.0) / 2, 3.0, 150.0, 40.0)];
toolBarLabel.font = [UIFont boldSystemFontOfSize:16.0];
toolBarLabel.textColor = [UIColor whiteColor];
toolBarLabel.textAlignment = NSTextAlignmentCenter;
toolBarLabel.backgroundColor = [UIColor clearColor];
toolBarLabel.text = @"Calculator";
prefToolBar = [[UIToolbar alloc] init];
[prefToolBar setTranslucent:NO];
prefToolBar.clipsToBounds = YES;
[[UIToolbar appearance] setBarTintColor:[UIColor colorWithRed:colorController.oRed/255.0 green:colorController.oGreen/255.0 blue:colorController.oBlue/255.0 alpha:1.0]];
[prefToolBar addSubview:toolBarLabel];
// add buttons
// create an array for the buttons
NSMutableArray* BarbuttonsArray = [[NSMutableArray alloc] initWithCapacity:5];
// create a Cancel button
UIBarButtonItem * cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Close"
style:UIBarButtonItemStylePlain
target:self
action:@selector(closeUIButton)];
cancelButton.style = UIBarButtonItemStylePlain;
cancelButton.tintColor = [UIColor whiteColor];
[BarbuttonsArray addObject:cancelButton];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[BarbuttonsArray addObject:flexible];
// create a submitButton
saveButton = [[UIBarButtonItem alloc]initWithTitle:@"Search"
style:UIBarButtonItemStylePlain
target:self
action:@selector(submitButton)];
saveButton.style = UIBarButtonItemStylePlain;
saveButton.tintColor = [UIColor whiteColor];
[BarbuttonsArray addObject:saveButton];
// put the BarbuttonsArray in the toolbar and release them
[prefToolBar setItems:BarbuttonsArray animated:NO];
UIView *frameToolbar = [[UIView alloc] initWithFrame:CGRectMake(0.0, 44.0, ScreenWidth, 0.5)];
frameToolbar.backgroundColor = [UIColor lightGrayColor];
[prefToolBar addSubview:frameToolbar];
更新
只需添加我用来返回屏幕宽度和高度的方法,以获取有关我在做什么的更多信息。
@implementation calcScreenSize
+(CGFloat)screenWidth {
if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)){
return [UIScreen mainScreen].bounds.size.width;
}else
return [UIScreen mainScreen].bounds.size.height;
}
+(CGFloat)screenHeight {
if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)){
return [UIScreen mainScreen].bounds.size.height;
}else
return [UIScreen mainScreen].bounds.size.width;
}