3

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

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;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

3 Answers3

1

You can force a layout of your toolbar in the viewDidAppear method of your viewController. I don't really like those kind of workarounds, but this seems to solve the similar issue I had correctly.

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

By the way, it seems that you are using a toolbar where a navigation bar (of a uinavigationcontroller) should be more appropriate.


EDIT : This fix in viewDidAppear was happening a little bit too late, causing the change to be visible for the user at the application startup.

It might not be adapted in your situation but, in mine, I ended up calling [self.toolbar setNeedsLayout] at the very end of my application didFinishLaunchingWithOptionsmethod (which is even uglier, but seems to work)

Paco_777
  • 216
  • 2
  • 10
  • To solve the problem I had to add the layoutSubviews call inside the method "viewDidLayoutSubviews" instead. – Seeler99 Dec 21 '16 at 00:07
  • Unfortunately `viewDidLayoutSubviews` does not work for me. But as I just explained in my edit, I finally ended up doing it in `didFinishLaunchingWithOptions` – Paco_777 Dec 05 '17 at 23:40
0

As explained in this thread your best bet is to subclass the UINavigationBar.

As alternative you can use UIButton instead. Here's the code:

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];
Community
  • 1
  • 1
carlodurso
  • 2,886
  • 4
  • 24
  • 37
0

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];
}
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
  • i had the same issue 2 days ago. i found no solution except changing space or inset of elements but thats not the proper way because if apple introduces some more phones and sizes then everything will break. So i came up with designing the whole thing using a xib with UIButtons and UILabels and managing them with constraints. – Mahesh Agrawal Dec 14 '16 at 05:32
  • i found the solution to use constraints differently from code to solve this issue. this is happening only when you are taking UIToolBar, UITabBar or UINavigationBar and applying constraints. – Mahesh Agrawal Dec 14 '16 at 11:59