IOS stage study 31st-day notes (UINavigationBar Introduction), iosuinavigationbar
IOS Learning (UI) knowledge point sorting
I. Introduction to UINavigationBar
1) concept: UINavigationBar is a class object used to define buttons in the navigation bar.
2) Before using UINavigationBar, you must first initialize the instance code in the navigation bar:
1 // initialize navigation bar 2 FirstViewController * firstVC = [[FirstViewController alloc] init]; 3 UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController: firstVC]; 4 self. window. rootViewController = nav; 5 6 // appearance be sure to use 7 // before initialization to modify the background color of the default UINavigationBar navigation bar, 8 [[UINavigationBar appearance] setBarTintColor: [UIColor yellowColor]; 9 10 // modify the default navigation bar text, that is, the icon color 11 [[UINavigationBar appearance] setTintColor: [UIColor blackColor]; 12 13 // set the navigation bar background image 14 [[UINavigationBar appearance] setBackgroundImage: [UIImage imageNamed: @ "icon"] forBarMetrics: UIBarMetricsDefault];
3) setNavigationBarHidden:
1 [self.navigationController setNavigationBarHidden:YES];
4) set the title of the navigation bar, for example:
1 self. title = @ "First View"; 2 // note: this title is also used if the View controller is not set.
5) titleView is used to set the view in the middle of the navigation bar, for example:
1 UIView * navBarView = [[UIView alloc] init]; 2 navBarView. frame = CGRectMake (0, 0,100, 30); 3 navBarView. backgroundColor = [UIColor clearColor]; 4 navBarView. layer. cornerRadius = 8.0f; 5 navBarView. clipsToBounds = YES; 6 7 UIButton * btn1 = [[UIButton alloc] init]; 8 btn1.frame = CGRectMake (0, 0, 50, 30); 9 btn1.backgroundColor = [UIColor blueColor]; 10 [btn1 setTitle: @ "message" forState: UIControlStateNormal]; 11 [btn1 addTarget: self action: @ selector (btn1Tapped :) forControlEvents: UIControlEventTouchUpInside]; 12 btn1.tag = 1000; 13 [navBarView addSubview: btn1]; 14 15 UIButton * btn2 = [[UIButton alloc] init]; 16 btn2.frame = CGRectMake (50, 0, 50, 30 ); 17 seconds = [UIColor blueColor]; 18 [btn2 setTitle: @ "phone" forState: UIControlStateNormal]; 19 [btn2 addTarget: self action: @ selector (btn1Tapped :) forControlEvents: Unknown]; 20 btn2.tag = 1001; 21 [navBarView addSubview: btn2]; 22 23 // Add our custom view in the middle of the navigation bar, 24 // the program will automatically center the view we set 25 self. navigationItem. titleView = navBarView;
6) buttons in the UIBarButtonItem navigation Bar.
/*
* The UIBarButtonSystemItemDone button style is text Done,
* The UIBarButtonSystemItemAdd button is the plus sign of the image.
* The UIBarButtonSystemItemCamera button is an image camera.
* UIBarButtonSystemItemFixedSpace is a placeholder. You can set the width.
* UIBarButtonSystemItemFlexibleSpace is a placeholder with a fixed width and a separate button in the navigation bar.
*/
For example:
1 // System camera button 2 UIBarButtonItem * button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCamera
Target: self action: @ selector (barButtonTapped :)]; 3 4 // placeholder button 5 UIBarButtonItem * button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
Target: self action: @ selector (barButtonTapped :)];
7) rightBarButtonItem:
1 self.navigationItem.rightBarButtonItem = button1;
8) rightBarButtonItems:
1 self.navigationItem.rightBarButtonItems = @[button2, button1];
9) edgesForExtendedLayout: the coordinates of the view are calculated from the lower left of the navigation bar to prevent the content area from being blocked by the navigation bar.
For example:
1 self.edgesForExtendedLayout = UIRectEdgeNone;
10) initWithTitle uses text as the navigation bar button, for example:
1 UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "return" style: UIBarButtonItemStylePlain
Target: self action: @ selector (back)];
11) initWithImage:
1 UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"st_logout"]
style:UIBarButtonItemStylePlain target:self action:@selector(back)];2 self.navigationItem.leftBarButtonItem = barButtonItem;
12) setToolbarHidden: Set display and hide in the navigation bar, for example:
1 [self.navigationController setToolbarHidden:NO];