putting labels, buttons on the navigation bar iOS

2019-02-18 05:53发布

问题:

I have created custom navigation controller,

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

I tried to add one label, but it does not work. Please show me a 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];

回答1:

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 :)



回答2:

add custom view in 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


回答3:

You can also achieve this from Interface Builder

1. First add a UIView which will look like following

2. Then add a UILabel inside the UIView which will look like following

3. In the Center It will look like following

Now add necessary constraint to meet your requirement.



回答4:

You need to add navigation item to the navigation bar. One per each navigation level. So you should 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];


回答5:

You need to change the titleView of the topItem. Like this:

naviBarObj.topItem.titleView = navLabel;

** update: Complete 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