【问题标题】:set cornerRadius and setbackgroundimage to UIButton将cornerRadius 和 setbackgroundimage 设置为 UIButton
【发布时间】:2013-06-28 08:54:31
【问题描述】:

我正在尝试设置 UIButton 的cornerRadius,但我不知道该怎么做。

如果我这样做:

button.layer.cornerRadius = 5;

效果很好,如果我喜欢这样的话:

button.layer.cornerRadius = 5;
[button setBackgroundColor:[UIColor colorWithPatternImage:radialGradient]];

角不圆。

我知道我可以解决这个问题

 [button.layer setMasksToBounds:YES];

但我专门寻找不同的解决方案,因为我在按钮上添加了一些箭头,如果我将遮罩设置为边界,则箭头被遮盖。

编辑:

radialGradient 具有函数功能

+ (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;
}

【问题讨论】:

  • 你可以显示 radialGradient 声明 >.?

标签: ios uibutton cornerradius setbackground


【解决方案1】:

如果您在按钮背景中使用图像,则需要将 clipsTobounds 设置为 true。

button.layer.cornerRadius = 10 
button.clipsToBounds = true

【讨论】:

    【解决方案2】:

    使用UIGraphicsImageRenderer,您可以使用UIGraphicsImageRendererContextcgContext属性访问CGContext并添加圆角路径:

    UIGraphicsImageRenderer(size: size).image { context in
        let rect = CGRect(origin: .zero, size: size)
        let clipPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
        context.cgContext.addPath(clipPath)
        context.cgContext.setFillColor(self.cgColor)
        context.cgContext.fillPath()
    }
    

    作为UIColor的扩展添加:

    extension UIColor {
        public func image(_ size: CGSize = CGSize(width: 10, height: 10), cornerRadius: CGFloat = 4) -> UIImage {
            return UIGraphicsImageRenderer(size: size).image { context in
                let rect = CGRect(origin: .zero, size: size)
                let clipPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
                context.cgContext.addPath(clipPath)
                context.cgContext.setFillColor(self.cgColor)
                context.cgContext.fillPath()
            }
        }
    }
    

    与使用 clipsToBounds 不同,此方法还允许您向按钮添加阴影。

    【讨论】:

      【解决方案3】:

      在 Identity Inspector 中为 Button 设置角半径。见图片

      在按钮的属性检查器中设置背景图像。见图片

      【讨论】:

        【解决方案4】:

        您还可以:

        btn.clipsToBounds = true;
        

        【讨论】:

        • 请注意,YES/NO 应与 Objective-C 一起使用,而 true/false 可用于 Swift。
        • @JAL 你也可以在 Objective-C 中使用true/TRUEfalse/FALSE,以及YESNO
        • Objective-C BOOL 类型最好使用 YES/NO,因为 true/false 映射到 C bool 类型。
        【解决方案5】:
        btn.clipsToBounds = YES; 
        

        我刚刚添加了这个,它对我有用。实际上,我可以通过将图像设置为特定的 UIButton 来将角半径设置为 UIButton。

        【讨论】:

        • 太棒了!现在我知道边界包括边界半径。
        【解决方案6】:

        下面是我如何通过代码创建按钮并设置其背景颜色。

        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];
        

        下面是与上述代码相关的按钮图片

        您可以随时使用代码并为背景和边框创建所需的颜色。希望对您有所帮助。

        【讨论】:

          【解决方案7】:

          //像这样创建按钮

               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];
          

          在此之前添加 QuartzCore 框架并在您的 .h 文件中导入 QuartzCore/CoreAnimation.h。

          希望对你有帮助..

          【讨论】:

          • 您好,感谢您的回复,但我特别不想要 clipToBounds。
          • 是的,但它没有走投无路:D
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-05
          • 1970-01-01
          • 2015-03-12
          • 1970-01-01
          相关资源
          最近更新 更多