【问题标题】:Wrong UIImageView dimensions on navigationItemnavigationItem 上的 UIImageView 尺寸错误
【发布时间】:2019-02-27 18:43:07
【问题描述】:

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

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 上。

我错过了什么?

【问题讨论】:

标签: ios objective-c xcode


【解决方案1】:

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

这是你目前遇到的情况(直接使用 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;
}

结果如下:

您可能已经注意到,这里的小技巧是您不想将图像视图直接添加为 UIBarButtonItem 的initWithCustomView 中的自定义视图。相反,您想使用外部视图来保存 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;
}

【讨论】:

    【解决方案2】:

    您可以使用简单的按钮并为其设置图像。

    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    [b setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
    [b addTarget:self action:@selector(randomMsg) forControlEvents:UIControlEventTouchUpInside];
    [b setImage:[UIImage imageNamed: Model.icon.MapList] forState:UIControlStateNormal];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:b];
    
    self.navigationItem.leftBarButtonItem = item;
    

    如果你有错误的纵横比,你可以使用

    [[b imageView] setContentMode: UIViewContentModeScaleAspectFit];
    

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多