ios - 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 上。
我错过了什么?
解决方案
我建议在你的图像视图中添加一个边框,这样你就可以看到发生了什么。
这是您当前遇到的情况(直接使用 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;
}
结果是:
您可能已经注意到,这里的小技巧是您不想将图像视图直接添加为initWithCustomView
UIBarButtonItem 中的自定义视图。相反,您想使用外部视图来保存 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;
}
推荐阅读
- apache-kafka - 当一个经纪人有问题时,解决这种情况的最佳方法是什么?
- android - 如何实现三个相邻的浮动动作按钮(Android XML)
- javascript - 如何使用每个循环动态添加数据
- python - 使用 Cloud Task、Cloud Functions 和 GAE 快速创建云任务
- python - wxPython 组合框的下拉菜单在弹出窗口上不起作用
- prolog - 根据属性生成 N (N - given) 的所有排列:对于每个 2<=i<=n 存在一个 1<=j<=i,因此 Prolog 中的 |v(i)-v(j)|=1
- appium - 如果我们使用 Appium 在飞行模式(Android)上运行代码,如何启用 wifi
- vue.js - audion HTML 5音频标签时出现Quasar错误
- python - Python:抓取 Instagram IGTV 数据,但仅显示前 24 条记录的信息
- excel - 打开excel工作簿时如何在特定列中自动包含公式?