1. 当你的UIButton 设置的是背景图片的时候,使用"currentBackgroundImage" 来获取 这个背景图片的宽高。
比如:
UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom]; [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal]; [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted]; [self addSubview:publishButton]; // 设置发布按钮的frame self.publishButton.bounds = CGRectMake(0, 0, self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height); self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
2. 当你的UIButton 使用的是图片的时候,使用“currentImage”,来获取图片的宽高,给 按钮赋值款宽高。
self.publishButton.bounds = CGRectMake(0, 0, self.publishButton.currentImage.size.width, self.publishButtoncurrentImage.size.height);