47 cornerradius uibutton ios setbackground
我试图设置UIButton的cornerRadius,但我不知道该怎么做.
如果我喜欢这样:
button.layer.cornerRadius = 5;
Run Code Online (Sandbox Code Playgroud)
效果很好,如果我这样做:
button.layer.cornerRadius = 5;
[button setBackgroundColor:[UIColor colorWithPatternImage:radialGradient]];
Run Code Online (Sandbox Code Playgroud)
角落不圆.
我知道我可以解决这个问题
[button.layer setMasksToBounds:YES];
Run Code Online (Sandbox Code Playgroud)
但我特意寻找不同的解决方案,因为我在按钮上添加了一些箭头,如果我将蒙版设置为边界,则箭头会被遮盖.
编辑:
radialGradient是whit func
+ (UIImage *)getRadialGradientImage:(CGSize)size centre:(CGPoint)centre radius:(float)radius startColor:(UIColor *)startColor endColor:(UIColor *)endColor{
// Initialise
UIGraphicsBeginImageContextWithOptions(size, YES, 1);
// Create the gradient's colours
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
const CGFloat *component_first = CGColorGetComponents([startColor CGColor]);
CGFloat red1 = component_first[0];
CGFloat green1 = component_first[1];
CGFloat blue1 = component_first[2];
const CGFloat *component_second = CGColorGetComponents([endColor CGColor]);
CGFloat red2 = component_second[0];
CGFloat green2 = component_second[1];
CGFloat blue2 = component_second[2];
const CGFloat components[8] = { red1,green1,blue1,1,red2,green2,blue2,1}; // End color
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);
// Normalise the 0-1 ranged inputs to the width of the image
CGPoint myCentrePoint = CGPointMake(centre.x * size.width, centre.y * size.height);
float myRadius = MIN(size.width, size.height) * radius;
// Draw it!
CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, myCentrePoint,
0, myCentrePoint, myRadius,
kCGGradientDrawsAfterEndLocation);
// Grab it as an autoreleased image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Clean up
CGColorSpaceRelease(myColorspace); // Necessary?
CGGradientRelease(myGradient); // Necessary?
UIGraphicsEndImageContext(); // Clean up
return image;
}
Run Code Online (Sandbox Code Playgroud)
Adr*_*n P 46
这是我如何通过代码创建一个按钮并设置其背景颜色.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:@"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
[self.view addSubview:btn];
Run Code Online (Sandbox Code Playgroud)
下面是与上述代码相关的按钮图像

您可以随时使用代码并创建背景和边框所需的颜色.希望这会帮助你.
小智 19
btn.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
我刚添加了这个,它对我有用.我实际上能够将角半径设置为UIButton,并将图像设置为特定的UIButton.
小智 6
如果您在按钮背景中使用图像,则需要设置clipsTobounds为 true。
button.layer.cornerRadius = 10
button.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
//像这样创建按钮
UIButton *cancel=[[UIButton alloc]initWithFrame:CGRectMake(9, 9,35,35)];
cancel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"BackX.png"]];
[cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cancel.layer setBorderColor: [[UIColor blackColor] CGColor]];
[cancel.layer setBorderWidth: 1.0];
cancel.contentMode=UIViewContentModeScaleAspectFill;
cancel.clipsToBounds=YES;
cancel.layer.cornerRadius=8.0;
[cancel addTarget:self action:@selector(cancelbtnclk1:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancel];
Run Code Online (Sandbox Code Playgroud)
在此之前,添加 QuartzCore Framework 并在 .h 文件中导入 QuartzCore/CoreAnimation.h。
希望它能帮助你..