HurkNburkS HurkNburkS - 6 months ago 21
iOS Question

UIToolBar buttons displaying partly off screen for iPhone 6

I have a UIView that contains a UIToolBar with two UIButtons and a Flexible space inbetween. For some weird reason on the iPhone 6 and 6Plus but not on any other model of iPhone the left and right UIButton in the UIToolBar appear to be displaying half on and half off of the screen.

here is an example of the issue I am having for the iPhone 6 / 6Plus

enter image description here

And here is the way it looks on every other i device

enter image description here

Here is the code I am using to create the UIToolBar, Buttons and Label.

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];


any help would be greatly appreciated.

UPDATE



Just adding the method I am using to return screen Width and Height, for further information on what I am doing.

@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;
}

Answer

Okay so I am still yet to find a reason for this happening on only the iPhone 6 and 6plus. However I have come up with a solution for myself, which effectivly puts padding in front of the left button and behind the right button..

I have a class tha returns the platform of the device and then use that to check if I need to insert these UIBarButtonSystemItemFixedSpace buttons to padd the visible buttons.

code is as follows.. this was the best I could do after hours of reading and coming up empty handed. I hope this helps anyone who happens to recive this error.

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];
}