how to make button round with image background in IPhone?

戏子无情 提交于 2020-01-01 06:14:25

问题


I have a round button, but i will make it have a backgroud with image.

but when i do it, by setting the (what) property to an image, the button becomes rectangle because the image is a rectangle. How do I keep the button round?


回答1:


Simply by doing this

#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;



回答2:


Tested Code:

.h

    -(void)vijayWithApple; 

.m

   -(void)vijayWithApple{

            NSLog(@"vijayWithApple Called");
   }


UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"Micky.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];

[button setTitle:@"Show View" forState:UIControlStateNormal];

button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value

button.clipsToBounds = YES;

button.layer.cornerRadius = 20;//half of the width

button.layer.borderColor=[UIColor redColor].CGColor;

button.layer.borderWidth=2.0f;

[self.view addSubview:button];

Restut




回答3:


Best solution I have found especially for UITableCell. I put the code in awakeFromNib and worked right off the bat.



来源:https://stackoverflow.com/questions/6338578/how-to-make-button-round-with-image-background-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!