IPhone custom buttons UINavigationBar

I have an application with 4 tabs. Each tab is a UINavigationController. The 4 UINavigationBar tabs should look the same, have a custom background image, a custom backButton, and a custom right button that launches the function.

I would like to make these settings only once in my code, and not in every RootViewController.

I managed to create a custom background image by pasting this code into my appDelegate:

    @implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"MyNavigationBar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

But I was not able to configure the back and right buttons or specify an action for the right button.

Is there a way to do this in appDelegate, as well as for the background image?
Or should I do the setup in every RootViewController?

+3
source share
3

viewWillAppear

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
UIImage *butImage = [[UIImage imageNamed:@"back.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];  
[button setBackgroundImage:butImage forState:UIControlStateNormal];  
[button addTarget:self action:@selector(gotoBack:) forControlEvents:UIControlEventTouchUpInside];  
button.frame = CGRectMake(0, 0, 48, 30);  
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];  
self.navigationItem.leftBarButtonItem = backButton;

backButton.

-(IBAction)gotoBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
+13

appdelegate aplicationDidFinishLaunching.....

UIImage *navBG = [UIImage imageNamed:@"barra-logo-centro.png"];

[[UINavigationBar appearance] setBackgroundImage:navBG forBarMetrics:UIBarMetricsDefault];

//backbutton: 22x30 , 44x60@2x

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage   imageNamed:@"back_button.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

 [[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:@"normal_button.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

proyect

+3

Renungas .

4 , UINavigationController.

( UITabBarController, ...) .

( ) :

    - (void)loadView {

      [super loadView];

      UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
      [button setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
      [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
      [button setFrame:CGRectMake(0, 0, 32, 32)];
      self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    }

    - (void)backAction {

        [self.navigationController popViewControllerAnimated:YES];  
    }

As you can see, all you have to do is override loadView and add a method to execute the popVievController selector.

Good luck

+1
source

Source: https://habr.com/ru/post/1789722/


All Articles