【问题标题】:navigationItem titleview align center between buttonsnavigationItem titleview在按钮之间对齐中心
【发布时间】:2015-05-08 13:58:08
【问题描述】:

我有喜欢的代码,在标题栏中我有 3 个按钮,左搜索按钮,右相机按钮和选项按钮,在两者之间我有标题。

我想要做的是,我想在搜索按钮和相机按钮之间显示标题,谁能帮我怎么做。

//Left Button
UIBarButtonItem *leftSearch = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_search"] style:UIBarButtonItemStyleBordered target:self action:@selector(gotoSearch)];

leftSearch.imageInsets = UIEdgeInsetsMake(0, -5.0f, 0, 0);

[leftSearch setTintColor:[UIColor whiteColor]];
/////////

//Right Button -- First Camera
UIBarButtonItem *rightCamera = [[UIBarButtonItem alloc]
                                initWithImage:[UIImage imageNamed:@"icon_camera"]
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(gotoPhotoGallery)];
[rightCamera setTintColor:[UIColor whiteColor]];
rightCamera.imageInsets = UIEdgeInsetsMake(0, 0, 0, -59.0f);
///////////////////

//Right second Button -- for options menu
UIBarButtonItem *rightMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"header_option"] style:UIBarButtonItemStyleBordered target:self action:@selector(gotoPhotoGallery)];
[rightMenu setTintColor:[UIColor whiteColor]];
rightMenu.imageInsets = UIEdgeInsetsMake(0, 0, 0, 5.0f);
///////////////////

NSArray *buttons = @[rightMenu, rightCamera];

self.navigationItem.rightBarButtonItems = buttons;

[[self navigationItem] setLeftBarButtonItem:leftSearch];

//Title Label

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"Museo Sans" size:20];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = LoginUserName;
self.navigationItem.titleView = label;
[label sizeToFit];

【问题讨论】:

    标签: objective-c uinavigationcontroller uibarbuttonitem uinavigationitem


    【解决方案1】:

    创建与导航栏宽度和高度相同的标题标签。例如

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,44)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = LoginUserName;
    titleLabel.textColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:titleLabel];
    

    这会将您的文本放在中间。

    【讨论】:

    • 我想要标题中心在搜索按钮和相机按钮之间,目前我的标题靠近搜索按钮,或者有时靠近相机按钮,因为标题看起来不太好。
    • 我已经修改了上面的答案。希望这对您更有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2015-08-24
    • 1970-01-01
    相关资源
    最近更新 更多