Custom color returned buttons for the navigation bar title generated by default in ios development
1. Modify the navigation bar color The page where the code of the navigation bar is stored
Self. navigationController. navigationBar. tintColor = [UIColor colorWithRed :( 21.0/255.0) green :( 153.0/255.0) blue :( 224.0/255.0) alpha: 1]; // Define the navigation bar color
Self. navigationItem. title = @ custom; // Define the title of the navigation bar
The second navigation bar comes with a return button. We need to customize its style. There are many ways to do this. For example, the view level in the Hack navigation bar. If you do not want the Hack navigation bar, you can use NavigationBarDelegate. The problem is that if it is the NavigationBar that comes with the navigation controller, you will not be able to access the NavigationBar (the program will Crash ). This is the description in the Apple document:
Note that if you use aUINavigationController object to manage hierarchical navigation, you shoshould notdirectly access the navigation bar object.
Here, we provide another "Custom" method. It may not be called customization, because we actually hide the default return button and provide a custom return button as the leftButton in the navigation bar. In this way, we can not only customize the button style (title and background image), but also trigger the custom method. The default return button action is popViewController. We can change it to another action.
This process is divided into four steps:
1. Hide the default return button. This is done by setting hidesBackButton of navigationItem to YES:
// Hide the default return button
[Self. navigationItemsetHidesBaCkButton: YES];
2. Customize a BarButtonItem. First, we create a UIButton. This UIButton is initialized using the buttonWithType: UIButtonTypeCustom method. Then, use the setBarckgroundImage method to customize the background image of the button and use the addTarget method to specify the event processing method of the button. In this way, we get a fully-Customized Button. BarButtonItem has an initWithCustomView: initialization method. We can use a custom view (such as a Custom Button) as the parameter of this method to construct a BarButtonItem.
// Customize the return button in the navigation bar
UIButton * btn = [UIButtonbuttonWithType: UIButtonTypeCustom];
Btn. frame = CGRectMake (15, 5, 38, 38 );
[Btn setBackgroundImage: [UIImage imageNamed: @ .png] forState: UIControlStateNormal];
[Btn addTarget: selfaction: @ selector (goBackAction) forControlEvents: UIControlEventTouchUpInsIde];
UIBarButtonItem * back = [[UIBarButtonItemalloc] initWithCustomView: btn];
3. Set BarButtonItem to the leftBarButton of navigationItem.
// Set the leftButton in the navigation bar
Self. navigationItem. leftBarButtonItem = back;
4. Compile the Event code of the Button.
-(Void) goBackAction {
// Add the custom action of the return button here
[Self. navigationControllerpopVIewControllerAnimated: YES];
}