【问题标题】:UIToolBar buttons displaying partly off screen for iPhone 6iPhone 6 的 UIToolBar 按钮部分显示在屏幕外
【发布时间】:2014-09-30 22:43:57
【问题描述】:

我有一个 UIView,它包含一个带有两个 UIButton 的 UIToolBar 和一个灵活的空间。由于某些奇怪的原因,在 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;
}

【问题讨论】:

  • 你能 NSLog 获取 ScreenWidth 吗?看看有没有不同?
  • @Bejibun 抱歉回复晚了,XCode 决定找不到我插入的 iPhone 6...所以修复了这个问题并记录了 ScreenWidth,它返回 320。

标签: ios uitoolbar iphone-6 iphone-6-plus


【解决方案1】:

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

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

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


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

它可能不适用于您的情况,但在我的情况下,我最终在application didFinishLaunchingWithOptionsmethod 的最后调用了[self.toolbar setNeedsLayout](这更难看,但似乎有效)

【讨论】:

  • 为了解决这个问题,我不得不在“viewDidLayoutSubviews”方法中添加 layoutSubviews 调用。
  • 不幸的是viewDidLayoutSubviews 对我不起作用。但正如我刚刚在编辑中解释的那样,我最终还是在didFinishLaunchingWithOptions
【解决方案2】:

正如this thread 中所述,您最好的选择是继承 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];

【讨论】:

  • 我不确定。。我不明白你为什么初始化 UIButton 框架,然后在下一行更改它的框架。
  • 已修复。你可能更喜欢这个版本。实际上,您正在寻求的解决方案是对另一个线程的引用。
  • 好的,谢谢,我现在好好研究一下,让你知道我过得怎么样。
【解决方案3】:

好的,所以我仍然没有找到仅在 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];
}

【讨论】:

  • 我在 2 天前遇到了同样的问题。除了改变空间或插入元素外,我没有找到任何解决方案,但这不是正确的方法,因为如果苹果推出更多手机和尺寸,那么一切都会中断。所以我想出了使用带有 UIButtons 和 UILabels 的 xib 来设计整个东西,并用约束来管理它们。
  • 我找到了使用与代码不同的约束来解决此问题的解决方案。仅当您使用 UIToolBar、UITabBar 或 UINavigationBar 并应用约束时才会发生这种情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多