在我的应用程序中,我希望导航栏显示标题和副标题.
在这种程度上,我将以下代码添加到我的视图控制器:
// Replace titleView
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease];
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = YES;
CGRect titleFrame = CGRectMake(0, 2, 200, 24);
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20];
titleView.textAlignment = UITextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = @"";
titleView.adjustsFontSizeToFitWidth = YES;
[_headerTitleSubtitleView addSubview:titleView];
CGRect subtitleFrame = CGRectMake(0, 24, 200, 44-24);
UILabel *subtitleView …Run Code Online (Sandbox Code Playgroud) iphone title subtitle uinavigationcontroller uinavigationitem
我想在我的UINavigationBar中有一个titleView,它有两行文本而不是一行
当我有一个"Back"-Button和一个"Edit"按钮时,我当前的实现效果很好,因为它看起来几乎居中.问题是我只有一个按钮.它看起来非常奇怪和错误,因为视图集中在可用空间上.
这是我目前的实施:
CGRect frame = self.navigationController.navigationBar.frame;
UIView *twoLineTitleView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(frame), 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, CGRectGetWidth(frame), 14)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text = self.title;
[twoLineTitleView addSubview:titleLabel];
UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 23, CGRectGetWidth(frame), 14)];
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
subTitleLabel.textAlignment = UITextAlignmentCenter;
subTitleLabel.text = self.subTitle;
[twoLineTitleView addSubview:subTitleLabel];
self.navigationItem.titleView = twoLineTitleView;
Run Code Online (Sandbox Code Playgroud) 我使用的是iOS 5的UIAppearance协议使用自定义导航栏后退按钮,当看到这里.与我发现的其他方法不同,这个方法是最好的,因为它保留了默认的后退按钮动画和行为.
唯一的问题是我无法更改其大小或将其设置为不剪辑子视图.这是发生了什么:

A是预期的行为,B是默认样式,C是剪切结果.
不幸的是,它并不像将UIBarButtonItem设置为clipsToBounds = NO那么容易.
有谁知道如何解决这个问题?谢谢!
我们有一个可能从几个地方推出的控制器,因此它的后退按钮可能有几个标题之一.它还有一个弹出另一个控制器的按钮.它们都具有相同的自定义标题元素,但它们的中心位置不同,因为第二个控制器没有后退按钮.我想添加一个垫片来解决这个问题,但我不知道它的宽度.如何获得UINavigationBar中后退按钮的宽度?
iphone objective-c uinavigationbar uinavigationcontroller uinavigationitem