How to programmatically set an image in NavBar?

I have a detailed view with a navigation bar with a back button and a name for the view. The navigation bar is installed programmatically. The name provided is defined as follows.

self.title = NSLocalizedString(name, @"");

The name depends on the presentation presented.

Now I would also like to introduce a small icon in the navigation bar, which also depends on the view.

How to do it?

0
source share
4 answers

You can set the backGround image in the navigationBar . Using this

Insert didFinishLaunchingWithOptions

 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"title_bar.png"] forBarMetrics:UIBarMetricsDefault]; 

Or you can set the image of the navigation bar in any view using

 UINavigationBar *navBar = [[self navigationController] navigationBar]; UIImage *image = [UIImage imageNamed:@"TopBar.png"]; [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 

Or you can set the view in your link navigation bar

 [[UINavigationBar appearance] addSubview:yourView]; 

or

 self.navigationItem.titleView = YourView; 

And set the title using h

 self.navigationItem.title = @"Your Title"; 

And you can get navigationBarButton using this

 -(void)getRightBarBtn { UIButton *Btn =[UIButton buttonWithType:UIButtonTypeCustom]; [Btn setFrame:CGRectMake(0.0f,0.0f,68.0f,30.0f)]; [Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"yourImage.png"]] forState:UIControlStateNormal]; //[Btn setTitle:@"OK" forState:UIControlStateNormal]; //Btn.titleLabel.font = [UIFont fontWithName:@"Georgia" size:14]; [Btn addTarget:self action:@selector(yourBtnPress:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:Btn]; [self.navigationItem setRightBarButtonItem:addButton]; } 

And set a simple image in the navigation bar

 UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"star.png"]]; UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:image]; self.navigationItem.rightBarButtonItem = backBarButton; 
+4
source

Add this code to viewDidLoad in your ViewController:

 UIImage *image = [UIImage imageNamed: @"myIcon.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.frame = CGRectMake(5, 2, 100, 39); [self.navigationController.navigationBar addSubview:imageView]; 

imageView can also be declared as an instance variable, so you can access it after adding it (for example, if you want to remove the icon from your navigation block at some point).

0
source

You need to subclass the UINavigationBar. Then in drawRect do something like:

 [[UIImage imageNamed...] drawInRect:...] 

and name the super thecourse method

0
source
 UIButton *homeBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, 28)]; [homeBtn setBackgroundImage:[UIImage imageNamed:@"homeBtn.png"] forState:UIControlStateNormal]; [homeBtn addTarget:self action:@selector(homeBtnPressed) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *navHomeBtn=[[[UIBarButtonItem alloc] initWithCustomView:homeBtn] autorelease]; self.navigationItem.rightBarButtonItem=navHomeBtn; [homeBtn release]; 
0
source

Source: https://habr.com/ru/post/1492345/


All Articles