问题
I have been trying to subclass the UINavigationController class for modifying things like background image, navigation bar size, back button, tittle font and so on. After a couple of hours and a lot of reading I have understand that I should not subclass the UINavigationController class...
I would like to have specific design and transfer that design to all screens that use my custom NavigationController...
SO far I haven't fount any elegant solution for this, most of the solutions are based on configuring something for showing in the next screen only (next view).
How can I achieve this? Maybe using categories? Thanks in advance.
回答1:
I am showing code for custom leftbarbutton of UINavigationcontroller
. You can add this code in each view controller and for both button( right and left)
leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:@"Yourcutomeimage.png"] forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton addTarget:self action:@selector(youraction:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton]autorelease];
//or
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton]autorelease];
Hope, this will help you..
回答2:
Yes @Nit is right, this is the way to customize the UINavigationController
, but if you don't want to do it for every UIViewController
you have, you can create a UIViewControllerBase
class, which is a subclass of UIViewController
and in viewDidLoad
method, set all your buttons the way @Nit has described and then make every UIViewController
you have to be a subclass of your UIViewControllerBase
class.
回答3:
If you want to add a custom back button to uinavigationcontroller you should use navigationItem.backBarButtonItem and implement it in the class where you initialize the uinavigationcontroller. Sample :-
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btnImage.png" style:UIBarButtonItemStyleBordered target:youtarget action:@sel(your action)];
self.navigationItem.backBarButtonItem = backButton; [backButton release];
回答4:
I have outlined my solution in this answer: https://stackoverflow.com/a/16831482/171933
Basically, you create a category on UIViewController and call one single method in every viewWillAppear
method of your view controllers.
来源:https://stackoverflow.com/questions/10516839/custom-back-button-for-navigationcontroller-for-every-screen