我的自定义UINavigationBar
在其titleView
中有一个2行标签:
UILabel *navBarLabel = [[UILabel alloc] initWithFrame:CGRectZero];
UINavigationItem *item = [[UINavigationItem alloc] init];
navBarLabel.backgroundColor = [UIColor clearColor];
navBarLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
navBarLabel.numberOfLines = 2;
navBarLabel.textAlignment = UITextAlignmentCenter;
navBarLabel.textColor = [UIColor colorWithRed:124.0/255.f green:125.0/255.f blue:128.0/255.f alpha:1.0];
navBarLabel.text = @"This\nis an example";
[navBarLabel sizeToFit];
item.titleView = navBarLabel;
但是我没有使用2行文本制作UILabel
,而是想在另一个上添加2 UILabels
,一个来实现字体的自定义分别在每个'线'中。它是如何实现的?有什么想法吗?
答案 0 :(得分:7)
只需创建一个父容器UIView
来保存两个标签,并使用此父容器作为标题视图。在伪代码中:
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
[titleView addSubview:someLabel];
[titleView addSubview:anotherLabel];
item.titleView = titleView;
...您需要确保正确设置标签框架,以便一个标签位于另一个标签上方。如何执行此操作取决于您是否使用自动布局,但这非常简单明了。
答案 1 :(得分:-2)
iOS允许您在栏中添加两个项目。请尝试以下代码。
UIButton *tempButton = nil;
UIImage *tempButtonImage = nil;
UIImage *tempButtonPressedImage = nil;
tempButtonImage = [UIImage imageNamed:@"unselected1.png"];
tempButtonPressedImage = [UIImage imageNamed:@"selected1.png"];
tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tempButton setImage : tempButtonImage forState : UIControlStateNormal];
[tempButton setImage : tempButtonPressedImage forState : UIControlStateHighlighted];
[tempButton addTarget : self action : @selector(tempButtonClick:) forControlEvents : UIControlEventTouchUpInside];
tempButton.frame = CGRectMake(0, 0, tempButtonImage.size.width, tempButtonImage.size.height);
UIView *tempButtonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, tempButtonImage.size.width, tempButtonImage.size.height}];
tempButtonContainer.backgroundColor = [UIColor clearColor];
[tempButtonContainer addSubview:tempButton];
UIBarButtonItem *tempToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:tempButtonContainer];
// custom logout button
UIButton *temp2Button = nil;
UIImage *temp2ButtonImage = nil;
UIImage *temp2ButtonPressedImage = nil;
temp2ButtonImage = [UIImage imageNamed:@"unselectedtemp2Button.png"];
temp2ButtonPressedImage = [UIImage imageNamed:@"selectedtemp2Button.png"];
temp2Button = [UIButton buttonWithType:UIButtonTypeCustom];
[temp2Button setImage : temp2ButtonImage forState : UIControlStateNormal];
[temp2Button setImage : temp2ButtonPressedImage forState : UIControlStateHighlighted];
[temp2Button addTarget : self action : @selector(temp2ButtonClick:) forControlEvents : UIControlEventTouchUpInside];
temp2Button.frame = CGRectMake(0, 0, temp2ButtonImage.size.width, temp2ButtonImage.size.height);
UIView *temp2ButtonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, temp2ButtonImage.size.width, temp2ButtonImage.size.height}];
temp2ButtonContainer.backgroundColor = [UIColor clearColor];
[temp2ButtonContainer addSubview:temp2Button];
UIBarButtonItem *temp2ToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:temp2ButtonContainer];
// setting right items
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:tempToolbarButton, temp2ToolbarButton, nil];
UIButton *button = nil;
UIImage *buttonImage = nil;
UIImage *buttonPressedImage = nil;
buttonImage = [UIImage imageNamed:@"asd.png"];
buttonPressedImage = [UIImage imageNamed:@"selected-asd.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage : buttonImage forState : UIControlStateNormal];
[button setImage : buttonPressedImage forState : UIControlStateHighlighted];
[button addTarget : self action : @selector(buttonClick:) forControlEvents : UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
UIView *buttonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, buttonImage.size.width, buttonImage.size.height}];
buttonContainer.backgroundColor = [UIColor clearColor];
[buttonContainer addSubview:button];
UIBarButtonItem *toolbarButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];
self.navigationItem.leftBarButtonItem = toolbarButton;