3

我有一个 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;
}
4

3 回答 3

1

您可以在 viewController 的 viewDidAppear 方法中强制工具栏的布局。我不太喜欢这种解决方法,但这似乎正确地解决了我遇到的类似问题。

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.toolbar layoutSubviews];
}

顺便说一句,您似乎正在使用一个工具栏,其中导航栏(uinavigationcontroller)应该更合适。


编辑:这个修复viewDidAppear发生得太晚了,导致在应用程序启动时用户可以看到更改。

[self.toolbar setNeedsLayout]它可能不适合您的情况,但在我的情况下,我最终在我的最后打电话application didFinishLaunchingWithOptionsmethod(这更丑陋,但似乎有效)

于 2016-06-03T16:12:37.697 回答
0

正如在这个线程中解释的那样,你最好的选择是继承 UINavigationBar。

作为替代方案,您可以改用 UIButton。这是代码:

UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
// close.frame = CGRectMake( 0, 0, 40, 30 );
[close setTitle:@"Close" forState:UIControlStateNormal];
[close addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:close];
于 2014-09-30T23:06:59.280 回答
0

好的,所以我仍然没有找到仅在 iPhone 6 和 6plus 上发生这种情况的原因。但是我为自己想出了一个解决方案,它有效地将填充放在左按钮前面和右按钮后面。

我有一个返回设备平台的类,然后使用它来检查是否需要插入这些 UIBarButtonSystemItemFixedSpace 按钮来填充可见按钮。

代码如下..这是我在阅读数小时后空手而归后能做的最好的事情。我希望这对碰巧收到此错误的任何人有所帮助。

platformCalculator = [[PlatformCalculator alloc] init];
NSString *currentPlatform = [platformCalculator platformNiceString];


if (([currentPlatform isEqual:@"IPHONE 6"]) || ([currentPlatform isEqual:@"IPHONE 6PLUS"])) {
    UIBarButtonItem *positiveSeparator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    positiveSeparator.width = 15;

    [BarbuttonsArray addObject:positiveSeparator];
}



if (([currentPlatform isEqual:@"IPHONE 6"]) || ([currentPlatform isEqual:@"IPHONE 6PLUS"])) {
    UIBarButtonItem *negativeSeparator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSeparator.width = 15;

    [BarbuttonsArray addObject:negativeSeparator];
}
于 2014-10-01T02:55:33.550 回答