I have the code below to add some UIBarButton to my project. This works great on the following:
Everything on iOS 7.1
- Simulator iPhone Retina 3.5 inches;
- Simulator iPhone Retina 4 inches;
- IPhone Retina Simulator 4 inches (64 bit)
- Iphone 4
- iPhone 4s
I do not have an iPhone 5 device for testing.
It does not work on the new iPhone 5s. What else?
Here is the code:
-(void)setupNavigationBar { self.saveSearchButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.saveSearchButton setImage:[UIImage imageNamed:@"Save Search"] forState:UIControlStateNormal]; [self.saveSearchButton setImage:[UIImage imageNamed:@"Save Search Active"] forState:UIControlStateHighlighted|UIControlStateSelected]; [self.saveSearchButton addTarget:self action:@selector(saveSearchButtonpressed)forControlEvents:UIControlEventTouchUpInside]; [self.saveSearchButton setFrame:CGRectMake(0, 0, 23, 31)]; self.changeLayutButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.changeLayutButton setImage:[UIImage imageNamed:@"View List"] forState:UIControlStateNormal]; [self.changeLayutButton setImage:[UIImage imageNamed:@"View Grid"] forState:UIControlStateHighlighted|UIControlStateSelected]; [self.changeLayutButton addTarget:self action:@selector(changeViewLayoutButtonPressed)forControlEvents:UIControlEventTouchUpInside]; [self.changeLayutButton setFrame:CGRectMake(0, 0, 23, 31)]; self.sortByButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.sortByButton setImage:[UIImage imageNamed:@"Sort By"] forState:UIControlStateNormal]; [self.sortByButton setImage:[UIImage imageNamed:@"Sort By Active"] forState:UIControlStateHighlighted|UIControlStateSelected]; [self.sortByButton addTarget:self action:@selector(sortByButtonPressed)forControlEvents:UIControlEventTouchUpInside]; [self.sortByButton setFrame:CGRectMake(0, 0, 23, 31)]; UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; fixedItem.width = 20; UIBarButtonItem *saveSearchButton = [[UIBarButtonItem alloc] initWithCustomView:self.saveSearchButton]; UIBarButtonItem *changeViewButton = [[UIBarButtonItem alloc] initWithCustomView:self.changeLayutButton]; UIBarButtonItem *sortByButton = [[UIBarButtonItem alloc] initWithCustomView:self.sortByButton]; NSArray *barbuttonitems = @[sortByButton, fixedItem, changeViewButton, fixedItem,saveSearchButton]; self.navigationItem.rightBarButtonItems = barbuttonitems; self.lastLayoutUsed = [[NSUserDefaults standardUserDefaults]objectForKey:@"LastLayoutUsed"]; if ([self.lastLayoutUsed isEqualToString:@"GridLayout"]){ self.changeLayutButton.selected = YES; [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(highlightButton:) userInfo:self.changeLayutButton repeats:NO]; } }
I went through the code and all the properties are not zero and have valid values. I also checked that all images are the right side.
How will this work on every device on the simulator, and not on the iPhone 5S?
Also, since the two phones (iPhone 4S and 5S) have the same screen width and iOS version, am I really puzzled?
Buttons are not displayed at all. Compiler warning and no console errors.
UPDATE
Tested the above code on iPhone 5 and it works great. This leads to the fact that it should be connected with the 64-bit iPhone 5S?
UPDATE 2
I removed all the code from the method and changed it to a very simple button:
self.saveSearchButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.saveSearchButton setTitle:@"Save" forState:UIControlStateNormal]; [self.saveSearchButton setTintColor:[UIColor blueColor]]; UIBarButtonItem *saveSearchButton = [[UIBarButtonItem alloc] initWithCustomView:self.saveSearchButton]; self.navigationItem.rightBarButtonItem = saveSearchButton;
Now it does not work on any devices or simulators.
What am I doing wrong here?
Update 3 - Solution!
That's right, it was one simple thing that caused all this fuss. I announced that my UIButtons are weak , not strong - did I get the impression that the user interface elements should be weak, as they often go out of view?
I came up with this answer using the comments section.
This also does not explain why it works on the iPhone 4S and iPhone 5, declared weak . He also worked on a 64-bit simulator declared as weak
Does this mean that the 64-bit simulator should be used as a guide, and the actual testing should be performed on the device, since it seems that the simulator is not accurate when it comes to testing UIKit ?
I would like to know more about this.