【发布时间】:2012-10-16 11:46:09
【问题描述】:
我想在 UINavigationBar 中添加一个 titleView,它有两行文本,而不是只有一行
当我有一个“后退”按钮和一个“编辑”按钮时,我当前的实现效果很好,因为它看起来几乎居中。问题是当我只有一个按钮时。它看起来非常奇怪和错误,因为视图以可用空间为中心。
UINavigationBar with left and right Button
UINavigationBar with only one Button
这是我当前的实现:
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;
【问题讨论】:
-
它将帮助你所有现有的场景stackoverflow.com/questions/37409260/…
标签: ios uinavigationbar titleview