right align self.navigationItem setTitleView
问题描述:
我需要将navigationItem TitleView对齐到最右侧。 (其中navigationItem rightBarButtonItem通常出现)right align self.navigationItem setTitleView
这怎么办?
我都试过,但没有运气仍然放在titleview的在中心:
CGRect titleViewPos = CGRectMake(300, 0, 200, 10);
self.navigationItem.titleView.frame = titleViewPos;
答
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
lbl.textAlignment = UITextAlignmentRight;
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"TITLEVIEW";
self.navigationItem.titleView = lbl;
你会得到输出
答
只是一个想法:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(300, 0, 200, 10);
[btn setTitle:@"titleView" forState:UIControlStateNormal];
self.navigationItem.titleView = btn;
答
您需要将UIElement(UIView,UILabel)传递给导航项目,然后您可以设置位置并使用导航标题或左侧按钮或右侧按钮进行其他自定义。 愿这能帮助你。
答
试试这个,可能是使用全
UILabel *lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = NSTextAlignmentRight;
lbNavTitle.backgroundColor = [UIColor clearColor];
lbNavTitle.text = @"hello World";
self.navigationItem.titleView = lbNavTitle;
答
于titleview框会自动调整,如果你想获得这样的结果,你可以试试这个:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:self.navigationController.navigationBar.frame];
[titleLabel setTextAlignment:UITextAlignmentRight];
[titleLabel setText:@"Your title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
self.navigationItem.titleView = titleLabel;
注意:一定不要设置你的视图控制器self.title否则你会覆盖titleView
另一种解决方案可以把titleLabel放入一个UIBarButtonItem(customView)并设置它喜欢self.navigationItem.rightBarButtonItem这样你就不需要设置文字对齐了,标签的框架可以是正确的文字大小
答
这一切都不适合我。我接受了Desdanova的建议,并将其添加为子视图。有道理,因为就是这样!用你想要的任何帧位置初始化它,然后
[self.view addSubView:lbl];
和Voila!
然后添加为子视图。 – Desdenova 2013-04-30 09:50:45