【问题标题】:iOS - Custom Navigation Bar with multiline titleiOS - 具有多行标题的自定义导航栏
【发布时间】:2017-06-26 21:11:41
【问题描述】:

谁能建议如何有效地实现下图中显示的内容?

总结一下:

  1. titleView 靠近Back 按钮对齐。我尝试设置 titleView 的框架,但它只在固定范围内变化
  2. title 是多行且左对齐的。我想这可以通过设置.textAlignmentnumberOfLines 属性来实现,大概是title label
  3. title.top 似乎与 backButton.top 对齐
  4. 标题下方的灰线不会是 topLayoutGuide,因为它似乎至少在长度上是可定制的
  5. 导航栏本身超过64p - 默认高度,还有可能是计算出来的?

到目前为止,我已经尝试在导航栏中添加一个带有UILabel 子视图的UIView,它有点工作,但看起来像

【问题讨论】:

  • 可能与this重复
  • 没有。问题不在于“多行”部分,而是标题相对于后退按钮的对齐方式以及导航栏本身的高度
  • this怎么样
  • 已经试过了,没用。感谢您指出:)

标签: ios xcode uinavigationbar


【解决方案1】:

你可以试试下面的代码。

目标c

UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.textColor = [UIColor blackColor];
lblTitle.text=@"This is a multiline string for the navBar details";
lblTitle.font=[UIFont systemFontOfSize:16];
lblTitle.frame = CGRectMake(0, 0, 150, 50);
lblTitle.numberOfLines=3;
self.navigationItem.titleView = lblTitle;

迅速

let label = UILabel(frame: CGRect(x:0, y:0, width:150, height:50))
    label.backgroundColor = UIColor.clear
    label.numberOfLines = 3
    label.font = UIFont.boldSystemFont(ofSize: 16.0)
    label.textColor = UIColor.blackColor
    label.text = "This is a multiline string for the navBar details"
    self.navigationItem.titleView = label

【讨论】:

  • @Dopapp 指出了这一点,正如我所提到的,我已经尝试过了。这只会将标签限制在 navigationItem 的 titleView 的固定范围内。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 2019-11-01
  • 2015-08-12
  • 1970-01-01
  • 2016-03-21
相关资源
最近更新 更多