首页 > 为什么我的UIbutton没有layer那个属性。本人小白

为什么我的UIbutton没有layer那个属性。本人小白

想设置一个圆形的按钮,不知道为啥没有layer那个属性
另外想请教一点
我以前按钮定位都是用的cgRectMake.如果想只设置位置,或者单独设置宽高应该怎么办呢?

若蒙眷顾 不胜惶恐


UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 100, 100);
btn.layer.cornerRadius = 50.0;

没法只设置位置。只能先把 frame 存起来。

    CGRect frame = button.frame;
    frame.origin = CGPointMake(x, y);    // 设置位置
    frame.size.width = 20;               // 设置宽度
    button.frame = frame;

可以写个 UIView 的 category,每次用的时候 include 进来,能省一些代码。

【热门文章】
【热门文章】