how to add items in uinavigationbar at a specific position?

后端 未结 6 856
予麋鹿
予麋鹿 2020-12-21 14:41

I am developing one application for iphone and in that i have some issues regarding adding more then one UILabel in navigation bar at a specific position.

6条回答
  •  别那么骄傲
    2020-12-21 15:30

    Try this code

    UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    buttonContainer.backgroundColor = [UIColor clearColor];
    UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
       [button0 setFrame: CGRectMake(160, 7 , 40,  30)];
    [button0 setTitle:@"Sort" forState:UIControlStateNormal];
    button0.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
    [button0 setBackgroundImage:[UIImage imageNamed:@"Btn_Sort_Btn_Sort_Places.png"]    forState:UIControlStateNormal];
    [button0 addTarget:self action:@selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
    [button0 setShowsTouchWhenHighlighted:YES];
    [buttonContainer addSubview:button0];
    self.navigationItem.titleView = buttonContainer;
    
    UILabel *lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 , 140, 40)];
    lbl_title.text = str_HeaderTitle;
    lbl_title.textColor = [UIColor whiteColor];
    lbl_title.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    lbl_title.backgroundColor = [UIColor clearColor];
    lbl_title.textAlignment   = NSTextAlignmentCenter;
    [buttonContainer addSubview:lbl_title];
    

提交回复
热议问题