putting labels, buttons on the navigation bar iOS

前端 未结 5 1679
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 11:59

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

相关标签:
5条回答
  • 2021-01-13 12:22

    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];
    
    0 讨论(0)
  • 2021-01-13 12:38

    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
    
    0 讨论(0)
  • 2021-01-13 12:43

    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
    
    0 讨论(0)
  • 2021-01-13 12:44

    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.

    0 讨论(0)
  • 2021-01-13 12:46
    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 :)

    0 讨论(0)
提交回复
热议问题