Setting labels, buttons on the iOS navigation bar

I created a custom navigation controller,

I want to add a date on the left, a back button on the right, and a title next to the back button.

I tried to add one shortcut, but it does not work. Please show me the way

UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)]; UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)]; navLabel.text = @"My Text"; [self.navigationController.navigationBar addSubview:navLabel]; [self.view addSubview:naviBarObj]; 
+4
source share
4 answers
 UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)]; UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)]; navLabel.text = @"My Text"; navLabel.textColor = [UIColor redColor]; [naviBarObj addSubview:navLabel]; [navLabel setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:naviBarObj]; 

try this, it will work. It works for me :)

+6
source

add custom view to UIToolbar

  UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f,190.0f, 44.01f)]; tools.barStyle = -1; // clear background NSMutableArray *buttons = [[NSMutableArray alloc] init]; UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; bi.width =10.0f; [buttons addObject:bi]; [buttons addObject:add your view]; bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; bi.width =10.0f; [buttons addObject:bi]; [buttons addObject:add your view]; [tools setItems:buttons animated:NO]; UIBarButtonItem *buttons_ = [[UIBarButtonItem alloc] initWithCustomView:tools]; self.navigationItem.rightBarButtonItem = buttons_; //same to leftBarButtonItem 
+2
source

You need to change the titleView topItem . Like this:

 naviBarObj.topItem.titleView = navLabel; 

** update: full code (tested and working) **

 UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(.0, .0, 320.0, 44.0)]; UINavigationItem *navItem = [[UINavigationItem alloc] init]; navItem.titleView = titleLbl; [naviBar pushNavigationItem: navItem animated: NO]; [navItem release]; // you don't need this for ARC [self.view addSubview: naviBar]; [naviBar release]; // you don't need this for ARC 
0
source

You need to add a navigation item to the navigation bar. One for each level of navigation. Therefore, you must have at least one of them.

** update **

 UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:@"Some title"]; /* * add buttons and labels */ [self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO]; 
0
source

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


All Articles