How to Set a background image for UINavigationBar in iPhone Development

Source: Internet
Author: User

IPhone DevelopmentIsUINavigationBarSetBackground ImageThe method is described in this article.IPhone Development, Sometimes we want to add a navigation barBackground ImageTo achieve a variety of navigation bar effects, other methods often fail to achieve the desired results, after online search and multiple experiments, determine the following best implementation scheme.

Add the following Category to UINavigatonBar:

 
 
  1. @implementation UINavigationBar (CustomImage)     
  2. - (void)drawRect:(CGRect)rect {     
  3.     UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];     
  4.     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];     
  5. }     
  6. @end    

For example, in my project, add the following code:

 
 
  1. /* input: The image and a tag to later identify the view */     
  2. @implementation UINavigationBar (CustomImage)     
  3. - (void)drawRect:(CGRect)rect {     
  4.     UIImage *image = [UIImage imageNamed: @"title_bg.png"];     
  5.     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];     
  6. }     
  7. @end     
  8.  
  9. @implementation FriendsPageViewController     
  10. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.     
  11. - (void)viewDidLoad {        
  12.     self.navigationBar.tintColor = [UIColor purpleColor];     
  13.          
  14.     [self initWithRootViewController:[[RegPageViewController alloc] init]];     
  15.     [super viewDidLoad];     
  16. }    

The effects are as follows:

Summary:IPhone DevelopmentIsUINavigationBarSetBackground ImageThe content of this method has been introduced. I hope this article will help you!

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.