我试图在文本太长时截断标签的尾部。一切都有效(附截图)。旋转到横向时出现问题。导航栏扩展为覆盖左右导航栏项。旋转回肖像时出现同样的问题。我在标签和导航栏周围放置边框以帮助说明(红色表示导航栏,蓝色表示标签)。有没有办法阻止导航栏重叠左右栏项目?
UILabel *broadcastLabel = [[UILabel alloc] initWithFrame:CGRectZero];
broadcastLabel.text = BILocalizedString(@"Broadcast with long titleeeeeeeeeeee");
[broadcastLabel sizeToFit];
broadcastLabel.textColor = BIColorForPaletteKey(BIColorPrimaryText);
broadcastLabel.font = BIFontForStyle(BIFontFixedNavigationStyle);
broadcastLabel.translatesAutoresizingMaskIntoConstraints = NO;
CGSize navBarSize = self.navigationController.navigationBar.layer.frame.size;
UIView *titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, navBarSize.width, navBarSize.height)];
[titleView addSubview:broadcastLabel];
self.navigationController.navigationItem.titleView = titleView;
titleView.layer.borderWidth = 1;
titleView.layer.borderColor = [UIColor redColor].CGColor;
broadcastLabel.layer.borderWidth = 1;
broadcastLabel.layer.borderColor = [UIColor blueColor].CGColor;
[titleView addConstraint: [NSLayoutConstraint constraintWithItem:broadcastLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:titleView
attribute:NSLayoutAttributeTop
multiplier:1.f
constant:4.f]];
[titleView addConstraint: [NSLayoutConstraint constraintWithItem:broadcastLabel
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:titleView
attribute:NSLayoutAttributeCenterX
multiplier:1.f
constant:0.f]];
[titleView addConstraint: [NSLayoutConstraint constraintWithItem:broadcastLabel
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:titleView
attribute:NSLayoutAttributeWidth
multiplier:1.f
constant:0.f]];
if (navBarSize.height >= tallSize)
{
/* Two lines*/
UILabel *onewayLabel = [[UILabel alloc] init];
onewayLabel.text = BILocalizedString(@"one-way message");
[onewayLabel sizeToFit];
onewayLabel.textColor = BIColorForPaletteKey(BIColorPrimaryText);
onewayLabel.font = BIFontForStyle(BIFontFixedSmallStyle);
onewayLabel.translatesAutoresizingMaskIntoConstraints = NO;
[titleView addSubview:onewayLabel];
[titleView addConstraint: [NSLayoutConstraint constraintWithItem:onewayLabel
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:titleView
attribute:NSLayoutAttributeCenterX
multiplier:1.f
constant:0.f]];
[titleView addConstraint: [NSLayoutConstraint constraintWithItem:onewayLabel
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:titleView
attribute:NSLayoutAttributeBottom
multiplier:1.f
constant:-4.f]];
[titleView addConstraint: [NSLayoutConstraint constraintWithItem:onewayLabel
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:titleView
attribute:NSLayoutAttributeWidth
multiplier:1.f
constant:0.f]];
}