首页 > 解决方案 > navigationItem 上的 UIImageView 尺寸错误

问题描述

我正在尝试在导航栏上添加图像,但尺寸错误。

UIImageView* menuButton=[[UIImageView alloc]initWithFrame:CGRectMake(4, 7, 20, 20)];
    menuButton.image=[UIImage imageNamed: Model.icon.MapList];
    UIBarButtonItem *accountBarItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
    self.navigationItem.leftBarButtonItem = accountBarItem;

它接缝我对图像的尺寸没有任何控制,因为当我更改 initWithFrame 的值时,没有任何变化。

此外,此问题仅出现在 iPhone 5 型号以上的 iPhone 上。

我错过了什么?

标签: iosobjective-cxcode

解决方案


我建议在你的图像视图中添加一个边框,这样你就可以看到发生了什么。

这是您当前遇到的情况(直接使用 imageView 作为栏按钮项的自定义视图):

- (void)_addImageViewToLeftButtonItemWithoutOuterView {
    UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Homer.jpg"]];
    [profileImageView setFrame:CGRectMake(0, 0, 40, 40)];
    profileImageView.layer.cornerRadius = 20.0f;
    profileImageView.layer.masksToBounds = YES;
    profileImageView.clipsToBounds = YES;
    profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
    profileImageView.layer.borderWidth = 1.0f;
    UIBarButtonItem *rbi = [[UIBarButtonItem alloc] initWithCustomView:profileImageView];
    self.navigationItem.leftBarButtonItem = rbi;
}

这是没有方面适合的样子:

没有方面适合的问题

并使用方面拟合集:

方面的问题

这就是我相信你想要的:

- (void)_addImageViewToLeftButtonItemWithOuterView {
    UIView *profileView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    profileView.layer.borderColor = [[UIColor redColor] CGColor];
    profileView.layer.borderWidth = 2.0f;
    UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Homer.jpg"]];
    profileImageView.contentMode = UIViewContentModeScaleAspectFit;
    [profileImageView setFrame:CGRectMake(0, 0, 40, 40)];
    profileImageView.layer.cornerRadius = 20.0f;
    profileImageView.layer.masksToBounds = YES;
    profileImageView.clipsToBounds = YES;
    profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
    profileImageView.layer.borderWidth = 1.0f;
    [profileView addSubview:profileImageView];
    UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:profileView];
    self.navigationItem.leftBarButtonItem = lbi;
}

结果是:

外部视图中的图像视图

您可能已经注意到,这里的小技巧是您不想将图像视图直接添加为initWithCustomViewUIBarButtonItem 中的自定义视图。相反,您想使用外部视图来保存 imageView,然后您可以调整子视图的框架以移动它。

如果您希望图像更靠右,这里还有一个示例(显然您不希望外部图像视图的边框,但我只是将其留在那里用于演示目的):

- (void)_addImageViewToLeftButtonItemWithOuterView {
    // adjust this to 80 width (it always starts at the origin of the bar button item
    UIView *profileView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    profileView.layer.borderColor = [[UIColor redColor] CGColor];
    profileView.layer.borderWidth = 2.0f;
    UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Homer.jpg"]];
    profileImageView.contentMode = UIViewContentModeScaleAspectFit;
    // and adjust this to be offset by 40 (move it to the right some)
    [profileImageView setFrame:CGRectMake(40, 0, 40, 40)];
    profileImageView.layer.cornerRadius = 20.0f;
    profileImageView.layer.masksToBounds = YES;
    profileImageView.clipsToBounds = YES;
    profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
    profileImageView.layer.borderWidth = 1.0f;
    [profileView addSubview:profileImageView];
    UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:profileView];
    self.navigationItem.leftBarButtonItem = lbi;
}

图像向右移动


推荐阅读