在导航栏中显示大量信息
Display a lot of informations in a navigation bar
我正在 Swift 1.2 和 Xcode 6.4 中开发一个项目。
我已经到了想要完全自定义导航栏的地步。主要目标是显示一些会不断变化的用户数据。为此,我需要有一个更大的导航栏,我可以在其中添加至少 6 个标签。
我想知道这是否可行,如何实现?
谢谢
我相信这段代码应该很容易通过添加多个字幕标签进行调整。您可以像这样向导航栏添加自定义标题视图:
- (void)updateTitle
{
if(self.hasAppeared) {
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 44)];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
titleLabel.text = convo[@"name"];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:@"Quicksand-Light" size:18];
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.minimumScaleFactor = 0.7;
[titleView addSubview:titleLabel];
UILabel *subtitleLabel = [[UILabel alloc] init];
subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
subtitleLabel.text = self.otherPersonString;
subtitleLabel.textAlignment = NSTextAlignmentCenter;
subtitleLabel.textColor = [UIColor whiteColor];
subtitleLabel.font = [UIFont fontWithName:@"Quicksand-Light" size:13];
subtitleLabel.adjustsFontSizeToFitWidth = YES;
subtitleLabel.minimumScaleFactor = 0.7;
[titleView addSubview:subtitleLabel];
NSDictionary *views = NSDictionaryOfVariableBindings(titleLabel, subtitleLabel);
[titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[titleLabel]-|" options:0 metrics:nil views:views]];
[titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[subtitleLabel]-|" options:0 metrics:nil views:views]];
[titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(-3)-[titleLabel]-(-2)-[subtitleLabel]-(1)-|" options:0 metrics:nil views:views]];
self.navigationItem.titleView = titleView;
}
}
但是,您将无法更改导航栏本身的高度,并且除非您重新创建此内容,否则此内容不会在整个应用程序中始终保留在导航栏中。您有什么理由不能将它添加到导航栏下方的新视图中?
我正在 Swift 1.2 和 Xcode 6.4 中开发一个项目。
我已经到了想要完全自定义导航栏的地步。主要目标是显示一些会不断变化的用户数据。为此,我需要有一个更大的导航栏,我可以在其中添加至少 6 个标签。
我想知道这是否可行,如何实现? 谢谢
我相信这段代码应该很容易通过添加多个字幕标签进行调整。您可以像这样向导航栏添加自定义标题视图:
- (void)updateTitle
{
if(self.hasAppeared) {
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 44)];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
titleLabel.text = convo[@"name"];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:@"Quicksand-Light" size:18];
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.minimumScaleFactor = 0.7;
[titleView addSubview:titleLabel];
UILabel *subtitleLabel = [[UILabel alloc] init];
subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
subtitleLabel.text = self.otherPersonString;
subtitleLabel.textAlignment = NSTextAlignmentCenter;
subtitleLabel.textColor = [UIColor whiteColor];
subtitleLabel.font = [UIFont fontWithName:@"Quicksand-Light" size:13];
subtitleLabel.adjustsFontSizeToFitWidth = YES;
subtitleLabel.minimumScaleFactor = 0.7;
[titleView addSubview:subtitleLabel];
NSDictionary *views = NSDictionaryOfVariableBindings(titleLabel, subtitleLabel);
[titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[titleLabel]-|" options:0 metrics:nil views:views]];
[titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[subtitleLabel]-|" options:0 metrics:nil views:views]];
[titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(-3)-[titleLabel]-(-2)-[subtitleLabel]-(1)-|" options:0 metrics:nil views:views]];
self.navigationItem.titleView = titleView;
}
}
但是,您将无法更改导航栏本身的高度,并且除非您重新创建此内容,否则此内容不会在整个应用程序中始终保留在导航栏中。您有什么理由不能将它添加到导航栏下方的新视图中?