1

我正在尝试在导航栏中显示我的徽标,但无法使其适合所有不同尺寸的手机。我可以让它适合一个人,但在另一个人身上看起来很糟糕。这是我最接近它看起来很好的地方。有谁知道我怎样才能使它看起来更好?

UIImage *logo = [UIImage imageNamed:@"WTB_LOGO"];
UIView *headerView = [[UIView alloc] init];
headerView.frame = CGRectMake(0, 0, 225, 115);

UIImageView *imgView = [[UIImageView alloc] initWithImage:logo];
imgView.frame = CGRectMake(0, 0, 205, 115);
imgView.contentMode = UIViewContentModeScaleAspectFit;

[headerView addSubview:imgView];

self.navigationItem.titleView = headerView;
4

3 回答 3

1

如果我理解这个问题,你的图像在 headerView 中没有正确居中。如果图像就是你想要的,根本不使用标题,titleView 会自动居中。

self.navigationItem.titleView = imgView;

如果你有一个后退按钮,它将推动图像视图。这是它的外观: 在此处输入图像描述 在此处输入图像描述

所以你必须让你的图像更小一点。

于 2015-07-10T15:29:25.430 回答
0

不知道为什么不简单地试试这个:

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];

或者我错过了什么?

于 2015-07-10T15:50:28.260 回答
0

您是否尝试过将其设置为:

...
UIView *headerView = [[UIView alloc] init];
headerView.frame = CGRectMake(0, 0, 225, 115);

...

// same frame with the headerview
imgView.frame = headerView.frame;
// Note: should be `imgView.frame = headerView.frame;`
//       `imgView.frame = CGRectMake(0, 0, 225, 115);` is different
//
// this also work if you are supporting multi-orientation
//
// or simply assign the imageView directly 
self.navigationItem.titleView = imgView;
// but the width is kinda broad `225` it will probably be push if you insert UIBarButtonItem to left/right

由于您正在使用UIViewContentModeScaleAspectFit它,它会自动缩放以适应框架。

如果您需要重新缩放图像,这里有一个代码。

+ (UIImage *)imageWithImage:(UIImage *)image scaleToSize:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}

希望这对您有所帮助。干杯! :)

于 2015-07-10T15:42:52.113 回答