如何使用图片的宽度和高度给UIButton 复制宽高
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);
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步