Ios: Modify the background color of NavigationController

Source: Internet
Author: User

Ios: Modify the background color of NavigationController

 

 

During ios development, we often need to modify the background color of NavigationController. When using [self. navigationController. when navigationBar setBackgroundColor: [UIColor redColor], the running result cannot be changed to Beijing color:

 

A new method is provided to solve this problem:

Write a NavigationBar to write a category:

 

@ Interface UINavigationBar (BackgroundColor)

 

// Set the background color of navigationBar

 

-(Void) lt_setBackgroundColor :( UIColor *) backgroundColor;

@ End

 


 

@ Implementation UINavigationBar (BackgroundColor)

 

Static char overlayKey;

 

-(UIView *) overlay

{

Return objc_getAssociatedObject (self, & overlayKey );

}

 

-(Void) setOverlay :( UIView *) overlay

{

Objc_setAssociatedObject (self, & overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC );

}

 

-(Void) lt_setBackgroundColor :( UIColor *) backgroundColor

{

If (! Self. overlay ){

[Self setBackgroundImage: [UIImage new] forBarMetrics: UIBarMetricsDefault];

[Self setShadowImage: [UIImage new]; // insert an overlay into the view hierarchy

Self. overlay = [[UIView alloc] initWithFrame: CGRectMake (0,-20, [UIScreen mainScreen]. bounds. size. width, 64)];

[Self insertSubview: self. overlay atIndex: 0];

}

Self. overlay. backgroundColor = backgroundColor;

}

@ End


 

 

Note: When Using objc_getAssociatedObject, You need to introduce the header file # import

 

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.