目前我只知道如何使用以下代码将字符串作为标题:
[self setTitle:@"iOS CONTROL"];
或者一张带有
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
但不可以同时进行
有解决方案吗?谢谢
目前我只知道如何使用以下代码将字符串作为标题:
[self setTitle:@"iOS CONTROL"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
但不可以同时进行
有解决方案吗?谢谢
您可以创建一个 UIView
并添加任意数量的子视图。例如:
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 40.0)];
titleView.backgroundColor = [UIColor clearColor];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor redColor];
titleLabel.text = @"My Title Label";
[titleLabel sizeToFit];
...
// Set the frames for imageView and titleLabel to whatever you need them to be
...
[titleView addSubview:imageView];
[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
UIView
,将一个UIImage
和一个UILabel
作为其子视图添加到其中,然后按照自己的方式将该UIView
添加使用self.navigationItem.titleView
。