标题图像在UINavigationBar中拉伸太多

时间:2013-07-29 03:46:15

标签: ios cocoa-touch uinavigationitem

目前我正在尝试为我的UINavigationBar设置标题图片,但它的范围很广。我正在使用以下代码:

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 40.0f, 20.0f);
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"titleImage.png"]];
    self.navigationItem.titleView = myImage;

有没有人知道它为什么伸展这么多宽度明智

4 个答案:

答案 0 :(得分:3)

你可以尝试在navigationItem或UIImageView上设置contentMode吗?

myImage.contentMode = UIViewContentModeAspectFill;

答案 1 :(得分:1)

只需使用以下代码,它就会将图像设置为导航栏的中心..

UIImage *image = [UIImage imageNamed:@"sclogo.png"];
      self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:image] autorelease];

答案 2 :(得分:0)

如果要在完整导航栏中设置图像,则可以使用以下代码: -

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"youimage"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

您正尝试在导航栏标题视图中设置图片。

答案 3 :(得分:0)

试试这个:

imageView.contentMode = UIViewContentModeScaleAspectFit;
相关问题