使用导航栏中的标题标签居中图像

时间:2016-11-10 11:01:16

标签: ios objective-c xcode uinavigationcontroller

我正在向导航栏添加自定义标题标签和自定义图像。我可以在我想要的位置正确添加标题标签,但我不能将水平图像居中到该标签。我正在添加一个屏幕截图和我使用的代码,可以提供帮助。

enter image description here enter image description here

-(void)addIconInNavBar
{
    CGSize imageSize = CGSizeMake(lblHeight, lblHeight);
    CGFloat marginX = (self.navigationController.navigationBar.frame.size.width) - (imageSize.width / 2);
    CGFloat marginY = (self.navigationController.navigationBar.frame.size.height / 2) - (imageSize.height / 2);

    imageView.frame = CGRectMake(marginX, marginY , imageSize.width, imageSize.height);
    [self.navigationController.navigationBar addSubview:imageView];
 }

3 个答案:

答案 0 :(得分:0)

我认为,更好的解决方案是使用https://serverfault.com/questions/329789/query-page-count-from-hp-web-jetadmin-10-2作为导航栏的标题。

UIView* customViewWithImage = [self createCustomView];
self.navigationItem.titleView = customViewWithImage;

您可以从custom view属性获取标题的属性,也可以从title attributes获取此信息。

答案 1 :(得分:0)

请将centerX更新为

CGFloat marginX = (self.navigationController.navigationBar.frame.size.width - imageSize.width) / 2;

或者

CGFloat marginX = (self.navigationController.navigationBar.frame.size.width/2) - (imageSize.width / 2);

答案 2 :(得分:0)

像这样使用它,

创建自定义视图并在自定义视图中添加标签和图片,并将此自定义视图添加为titleView

needs a .yo-rc.json configuration file made by JHipster.