How do I vertically correct the navigationBar's titleView text position when using a custom font?

坚强是说给别人听的谎言 提交于 2019-12-04 18:25:57

问题


We're using custom fonts for the titleView in the navigation bar. Somehow Apple always draws this font too high.

How do I correct for this strange offset you get when you are using custom fonts in a navbar?


回答1:


I used setTitleVerticalPositionAdjustment:forBarMetrics:.

Compatibility: available starting from iOS 5.




回答2:


Your can set a new view as titleView, then add a new label to it:

UIView * customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 40.0f)];

UILabel * customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 200.0f, 20.0f)];
[customLabel setBackgroundColor:[UIColor clearColor]];
[customLabel setTextColor:[UIColor whiteColor]];
[customLabel setFont:[UIFont systemFontOfSize:12.0f]];
[customLabel setTextAlignment:UITextAlignmentCenter];
[customLabel setText:@"Your Text"];
[customTitleView addSubview:customLabel];
[customLabel release];

[self.navigationItem setTitleView:customTitleView];
[customTitleView release];


来源:https://stackoverflow.com/questions/8475332/how-do-i-vertically-correct-the-navigationbars-titleview-text-position-when-usi

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