Create a subtitle in navigationbar

寵の児 提交于 2019-12-23 09:56:30

问题


In iOS you can create a title with:

self.navBar.title = @"";

where I set navBar in the header file.

Is there also something for the subTitle / Description?

I found description when I typed a . after self.navBar and thought maybe I could do something with this?


回答1:


Use the self.navBar.navigationItem.prompt = @"This is the subtitle";

It's from UIKit in the base UINavigationViewController.




回答2:


Objective-C code for the solution

UILable *title = [[UILabel alloc]init];
UILabel *subtitle = [[UILabel alloc]init];

[title setFont:[UIFont systemFontOfSize:12]];
[title setTextColor:[UIColor whiteColor]];
[title setFont:[UIFont systemFontOfSize:17]];
[title sizeToFit];
title.text = @"Title";

[subtitle setTextColor:[UIColor whiteColor]];
[subtitle setFont:[UIFont systemFontOfSize:12]];
[subtitle setTextAlignment:NSTextAlignmentCenter];
[subtitle sizeToFit];
subtitle.text = @"Subtitle Title";

UIStackView *stackVw = [[UIStackView alloc]initWithArrangedSubviews:@[title,subtitle]];
stackVw.distribution = UIStackViewDistributionEqualCentering;
stackVw.axis = UILayoutConstraintAxisVertical;
stackVw.alignment =UIStackViewAlignmentCenter;


[stackVw setFrame:CGRectMake(0, 0, MAX(title.frame.size.width, subtitle.frame.size.width), 35)];
self.navigationItem.titleView = stackVw;


来源:https://stackoverflow.com/questions/29626710/create-a-subtitle-in-navigationbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!