UIButton 设置 imageView frame
来源:互联网 发布:css 实战手册 知乎 编辑:程序博客网 时间:2024/05/22 10:43
我们发现为UIButton的imageView设置frame时,是无法改变大小的,大小就是图片的大小,位置的话可以利用imageEdgeInsets属性修改位置。
原理:修改UIButton imageView的frame会调用superView也就是UIButton的- (void)layoutSubviews,而这个layoutSubviews会调用UIButton的
- (CGRect)imageRectForContentRect:(CGRect)contentRect; 方法,这个方法默认返回图片的size。
修改imageView的frame的方法有几种,我想到的这几种:
1. 重写 - (CGRect)imageRectForContentRect:(CGRect)contentRect;
2. 删除imageView视图,添加新的imageView,控制frame
3. 重写 layoutSubviews
- (void)layoutSubviews
{
CGRect frame =self.imageView.frame;
[superlayoutSubviews];
self.imageView.frame = frame;
}
4. 不继承UIButton,直接修改layoutSubviews的默认实现
void layoutSubviewsWithoutAutoSetImageViewFrame(idself, SEL sel)
{
UIButton * btn = (UIButton*)self;
CGRect frame;
if (btn.imageView)
{
frame = btn.imageView.frame;
}
[btn performSelector:@selector(oldLayoutSubviews)];
if (btn.imageView)
{
btn.imageView.frame = frame;
}
}
void replaceUIButtonLayoutSubviewsMethod()
{
IMP layoutSubviewsImp = class_getMethodImplementation([UIButtonclass], @selector(layoutSubviews));
class_addMethod([UIButtonclass], @selector(oldLayoutSubviews), layoutSubviewsImp,"v@:");
class_replaceMethod([UIButtonclass],@selector(layoutSubviews), (IMP)(layoutSubviewsWithoutAutoSetImageViewFrame),"v@:");
}
欢迎大家支出问题,共同讨论,共同进步,谢谢。
- UIButton 设置 imageView frame
- UIButton设置frame没有变化?
- UIButton 上设置 self.imageView self.titleLabel
- 设置UIButton中titleLabel和ImageView的位置
- UIButton在设置Image时,会把之前设置的frame给覆盖掉。
- iOS设置UIButton的titleLabel和imageView需要注意的一些小问题
- UIButton的Title和frame的BUG
- 重写UIButton的图片、title Frame
- iOS initWithFrame:frame] 与 [UIButton buttonWithType] 对比
- 自定义UIButton图片和文字的frame
- UIButton.titleLabel.text和[UIButton setTitle:],UIButton.imageView.image和[UIButton setImage]的区别
- 如何使UIButton中的imageview旋转
- iOS UIButton imageView图片无法显示
- UIButton自带的imageView的显示
- UIButton的ImageView与TitleLabel对齐
- iOS修改UIButton内部imageView的尺寸
- ImageView让图片像素延伸填满frame
- 设置UIButton字体大小
- 安全技术
- oracle的购买价格研究(三)-附x86价格
- 为什么好的博客越来越少?
- NOOBS安装自定义系统
- 操作系统
- UIButton 设置 imageView frame
- 错误: 在类 com.zs.container.CollectionData 中找不到主方法, 请将主方法定义为: public static void main(String[] args)
- 数据挖掘十大算法之一C4.5(详细版终结版)
- 算法导论:快速找出无序数组中第k小的数
- 随感——2015年3月小结
- Primefaces Components and the @ConversationScoped Beans
- android实用代码片段(二)
- IOS越狱开发好demo
- GITBash 使用 给新手的一些经验