How to set rectangle border for custom type UIButton

前端 未结 6 557
臣服心动
臣服心动 2020-12-28 08:55

I am trying to set an image as the background of a custom UIButton. I was able to set a background image for the \"rounded rect\" UIButton in interface builder, but now the

6条回答
  •  没有蜡笔的小新
    2020-12-28 09:49

    Check out the cornerRadius, borderWidth and borderColor properties of CALayer. These properties are new as of iPhone OS 3.0.

    You probably want to subclass UIButton and then set these properties in the constructor. Your button has a layer property you can access to set these border properties.

    Here's an example:

    - (id)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
    
        [self.layer setBorderWidth:1.0];
        [self.layer setCornerRadius:5.0];
        [self.layer setBorderColor:[[UIColor colorWithWhite:0.3 alpha:0.7] CGColor]];
    }
    
    return self;
    }
    

提交回复
热议问题