putting labels, buttons on the navigation bar iOS

2019-02-18 06:05发布

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];

5条回答
Bombasti
2楼-- · 2019-02-18 06:12
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 :)

查看更多
做个烂人
3楼-- · 2019-02-18 06:18

You can also achieve this from Interface Builder

1. First add a UIView which will look like following

enter image description here

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

enter image description here

3. In the Center It will look like following

enter image description here

Now add necessary constraint to meet your requirement.

查看更多
迷人小祖宗
4楼-- · 2019-02-18 06:19

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
查看更多
▲ chillily
5楼-- · 2019-02-18 06:32

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
查看更多
Animai°情兽
6楼-- · 2019-02-18 06:32

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];
查看更多
登录 后发表回答