Ios navigation bar (custom and system-based)

Source: Internet
Author: User

Ios navigation bar (custom and system-based)

System Mode:

// 1. set the navigation bar background image [self. navigationController. navigationBar setBackgroundImage: [[UIImage alloc] init] forBarMetrics: UIBarMetricsDefault]; self. navigationController. navigationBar. shadowImage = [[UIImage alloc] init]; [[self navigationController] setNavigationBarHidden: NO animated: YES]; self. navigationController. navigationBar. backgroundColor = [[UIColor alloc] initWithRed: 248/255. 0 green: 248/255. 0 blue: 248 /255.0 alpha: 1.0]; // 2. UIButton * cancelButton = [UIButton buttonWithType: UIButtonTypeRoundedRect] on the left of the navigation Panel; if (cancelButton! = Nil) {[cancelButton setTitle: POST_CANCEL_BUTTON forState: UIControlStateNormal]; [cancelButton setFrame: CGRectMake (0, 0, WIDTH_SCREEN/5.0, 44)]; [cancelButton setTitleColor: [[UIColor alloc] initWithRed: 0 green: 158/255. 0 blue: 150/255. 0 alpha: 1.0] forState: UIControlStateNormal]; cancelButton. titleLabel. font = [UIFont systemFontOfSize: 16.0]; cancelButton. contentHorizontalAlignment = UIControlConten Parameters; [cancelButton addTarget: self action: @ selector (optional) forControlEvents: Events]; UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView: cancelButton]; if! = Nil) {self. navigationItem. leftBarButtonItem = leftItem;} // 3. The publish button UIButton * postButton = [UIButton buttonWithType: Success] on the right of the navigation Panel; if (postButton! = Nil) {[postButton setFrame: CGRectMake (0, 0, WIDTH_SCREEN/5.0, 44)]; [postButton setTitle: @ "publish" forState: UIControlStateNormal]; [postButton setTitleColor: [[UIColor alloc] initWithRed: 0 green: 158/255. 0 blue: 150/255. 0 alpha: 1.0] forState: UIControlStateNormal]; postButton. titleLabel. font = [UIFont systemFontOfSize: 16.0]; postButton. contentHorizontalAlignment = UIControlContentHorizontalAlignmentRigh T; [postButton addTarget: self action: @ selector (role) forControlEvents: role]; UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView: postButton]; if (rightItem! = Nil) {self. navigationItem. rightBarButtonItem = rightItem;} // 4. text UILabel * navigationLabel = [[UILabel alloc] initWithFrame: CGRectMake (0, 0, 0, 44)]; if (navigationLabel! = Nil) {[navigationLabel setTextColor: [UIColor blackColor]; navigationLabel. text = POST_NAVIGATION_TITLE; [navigationLabel setTextAlignment: NSTextAlignmentCenter]; navigationLabel. font = [UIFont systemFontOfSize: 18.0]; self. navigationItem. titleView = navigationLabel;} // 5. UIView * line = [[UIView alloc] initWithFrame: CGRectMake (0, 20 + 44, WIDTH_SCREEN, 1)]; if (line! = Nil) {line. backgroundColor = [[UIColor alloc] initWithRed: 221/255. 0 green: 221/255. 0 blue: 221/255. 0 alpha: 1.0]; [self. view addSubview: line];}

Custom:

// 1. Create the navigation bar view UIView * navView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, WIDTH_SCREEN, 65)]; if (navView! = Nil) // This method is introduced when the navigation view is not loaded successfully {// 1. set the background navView for the navigation view. backgroundColor = [UIColor colorWithRed: 248/255 .0 green: 248/255 .0 blue: 248/255 .0 alpha: 1]; [[self navigationController] setNavigationBarHidden: YES animated: YES]; // 2. UIButton * leftButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; if (leftButton! = Nil) {leftButton. frame = CGRectMake (15, 20, 65, 44); [leftButton setTitle: POST_CANCEL_BUTTON forState: UIControlStateNormal]; [leftButton setTitleColor: [[UIColor alloc] initWithRed: 0 green: 158/255. 0 blue: 150/255. 0 alpha: 1.0] forState: UIControlStateNormal]; leftButton. titleLabel. font = [UIFont systemFontOfSize: 16.0]; leftButton. contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [LeftButton addTarget: self action: @ selector (cancelButtonEventTouchUpInside) forControlEvents: UIControlEventTouchUpInside]; [navView addSubview: leftButton];} // 3. UIButton * rightButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; if (rightButton! = Nil) {[rightButton setFrame: CGRectMake (WIDTH_SCREEN-80, 20, 65, 44)]; [rightButton setTitle: @ "publish" forState: UIControlStateNormal]; [rightButton setTitleColor: [[UIColor alloc] initWithRed: 0 green: 158/255. 0 blue: 150/255. 0 alpha: 1.0] forState: UIControlStateNormal]; rightButton. titleLabel. font = [UIFont systemFontOfSize: 16.0]; rightButton. contentHorizontalAlignment = UIControlContentHorizontalAlign MentRight; [rightButton addTarget: self action: @ selector (postButtonEventTouchUpInside) forControlEvents: UIControlEventTouchUpInside]; [navView addSubview: rightButton];} // 4. text UILabel * navTitle = [[UILabel alloc] initWithFrame: CGRectMake (80, 20, WIDTH_SCREEN-80-80, 44)]; if (navTitle! = Nil) {[navTitle setTextColor: [UIColor blackColor]; navTitle. text = POST_NAVIGATION_TITLE; [navTitle setTextAlignment: NSTextAlignmentCenter]; navTitle. font = [UIFont systemFontOfSize: 18.0]; [navView addSubview: navTitle];} // 5. add the separation line UIView * navDividingLine = [[UIView alloc] init]; if (navDividingLine! = Nil) {navDividingLine. frame = CGRectMake (0, 20 + 44, WIDTH_SCREEN, 1); navDividingLine. backgroundColor = [UIColor colorWithRed: 221/255 .0 green: 221/255 .0 blue: 221/255 .0 alpha: 1]; [navView addSubview: navDividingLine];} // 6. add the navigation bar [self. view addSubview: navView];}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.