繁体   English   中英

在UINavigationBar上放置一个大徽标作为标题视图 - iOS

[英]Putting a large logo on UINavigationBar as the title view - iOS

我已使用此代码在导航栏上成功添加了我的徽标;

UIImage *image = [UIImage imageNamed: @"top-logo"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];

self.navigationItem.titleView = imageView;

问题是我的logo的高度为54像素 ,导航栏的默认值为44像素 徽标是故意设计为从导航栏的底部溢出,但我必须更改导航栏的界限才能这样做,我不想在Apple的指南上运行。 但是我需要imageView这个导航项的titleView从导航栏中溢出。

此外,对于我的一个应用程序,我减少了导航栏的高度,当应用程序转到背景并返回时,它开始变得有趣(高度开始变回正常,导致导航栏中出现黑色背景)。

这是一个类似情况的帖子。 接受的答案使用了UIButton而不是imageview。

接受答案的代码:

- (void)viewDidLoad {
  [super viewDidLoad];

  UIButton *logoView = [[[UIButton alloc] initWithFrame:CGRectMake(0,0,85,40)] autorelease];
  [logoView setBackgroundImage:[UIImage imageNamed:@"navBarLogo.png"] forState:UIControlStateNormal];
  [logoView setUserInteractionEnabled:NO];

  self.navigationItem.titleView = logoView;
}

TTLauncherView导航栏中的图像而不是标题

首先,在-viewDidLoad方法中,创建一个适合NavigationBar大小的UIImageView。 之后,将您的徽标放在UIImageView中,并将其设置为titleView。 代码如下:

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)];
image.contentMode = UIViewContentModeScaleAspectFit;
[image setImage: [UIImage imageNamed:@"logo"]];
self.navigationItem.titleView = image;

我正在寻找一个类似的解决方案,但我尝试了一些不同的东西并完美地工作:

-(void)viewDidLoad
{

  [super viewDidLoad];

  //Set the place where you want your logo to appear on your navigation bar (just an example):

  UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(130, 20, 50,  54)];
  imageView.image = [UIImage imageNamed:@"yourImage@2x.png"];
  [self.navigationController.view addSubview:imageView];

}

如果您只是想找到一种方法将图像添加到导航栏并且您正在使用Swift,那么这就是您需要的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let imageView = UIImageView(image: UIImage(named: "your_logo"))
    navigationItem.titleView = imageView
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM