I have been trying for several hours to make the left button work correctly and simulate the back button.
My code for creating a button:
UIBarButtonItem *backButton = [self customBarButton:@"back_button" imageHiglighted:@"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6]; backButton.target = self; backButton.action = @selector(buttonPressed:); self.navigationItem.leftBarButtonItem = backButton;
Here is the method called to create the custom button:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)xy:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider { UIImage *customImageDefault = [UIImage imageNamed:imageDefault]; UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted]; CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider); UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton]; [customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal]; [customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted]; UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton]; return barCustomButton; }
And the action:
-(void)buttonPressed:(id) sender{ NSLog(@"Entered"); SearchViewController *ViewController = [[SearchViewController alloc] init]; [self.navigationController pushViewController:ViewController animated:YES]; }
So, I was able to do this with a simple UIButton, but not with UIButtonBarItem, and I really don't know what is happening to it.
If you could help me, I would be very grateful.
Thanks.
source share