Normaly 你应该使用contentMode
(在这种情况下UIViewContentModeScaleAspectFit
)每个 UIView 子类都有这个属性,通常你可以用它来UIImageView
。但我几乎可以肯定这不适用于UIBarButtonItem
. 试试看,但你可能会碰壁。
如果我的预测是真的,你必须以编程方式调整图像大小:
UIImage *myImage = [UIImage imageNamed:@"fb_share"];
float maximumHeight = 44;
float ratio = maximumHeight/ myImage.size.height;
CGSize imageNewSize = CGSizeMake(myImage.size.width*ratio, maximumHeight);
UIGraphicsBeginImageContextWithOptions(imageNewSize, NO, 0.0);
[myImage drawInRect:CGRectMake(0, 0, imageNewSize.width, imageNewSize.height)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *Share = [[UIBarButtonItem alloc] initWithImage:resizedImage style:UIBarButtonItemStyleBordered target:self action:@selector(Share:)];