使用标题标签在导航栏中定位图像
问题描述:
我将自定义标题标签和定制图像添加到导航栏。我可以在我想要的位置上正确添加标题标签,但我无法将横向图像添加到该标签。我添加了一个屏幕截图和我可以使用的代码。使用标题标签在导航栏中定位图像
-(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];
}
答
我认为,更好的解决办法是使用custom view导航栏的标题。
UIView* customViewWithImage = [self createCustomView];
self.navigationItem.titleView = customViewWithImage;
你可以从title attributes财产所有权的属性,你也可以得到appearance此信息。
答
为的centerX请更新为
CGFloat marginX = (self.navigationController.navigationBar.frame.size.width - imageSize.width)/2;
或者
CGFloat marginX = (self.navigationController.navigationBar.frame.size.width/2) - (imageSize.width/2);
答
这样使用它,
创建自定义视图和自定义视图中添加标签和图像,并添加这个定制查看为标题查看
UIView *vw = [[UIView alloc] initWithFrame:yourFrame];
UILabel *lbl = [[UILabel alloc] initWithFrame:lblFrame];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:imgFrame];
[vw addSubView:lbl];
[vw addSubView:imgView];
self.navigationItem.titleView = vw;
我想居中到自定义标题不居中导航栏。 – birdcage