我想在导航栏上添加图片,但尺寸不对。
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 上。
我错过了什么?
最佳答案
您可以只使用简单的按钮并为其设置图像。
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];
关于ios - navigationItem 上的 UIImageView 尺寸错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52476192/