【问题标题】:UIButton With Rounded Corner On Top顶部有圆角的 UIButton
【发布时间】:2023-04-01 22:30:01
【问题描述】:

我需要一个带有背景图像、顶部圆角和阴影的 UIButton。这是我的代码

我尝试了许多堆栈溢出的帖子,但没有一个对我有用。

我能够使用以下方法添加具有圆角和阴影背景图像的 UIButton。

CALayer *imageLayer = [imageButton layer];
imageLayer.frame =  CGRectMake(110, 190, 100, 100);
imageLayer.cornerRadius = 10.0; 
imageLayer.shadowColor = [UIColor grayColor].CGColor;
imageLayer.shadowOpacity = 1.0;
imageLayer.shadowRadius = 1;
imageLayer.shadowOffset = CGSizeMake(03.0f, 08.0f);
imageLayer.masksToBounds = YES;


CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor blueColor].CGColor;
sublayer.shadowOffset = CGSizeMake(5, 8);
sublayer.shadowRadius = 2.0;
sublayer.shadowColor = [UIColor grayColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = imageLayer.frame;
sublayer.borderColor = [UIColor blackColor].CGColor;
sublayer.borderWidth = 0.0;
sublayer.cornerRadius = 10.0;

[imageButton.layer.superlayer insertSublayer:sublayer atIndex:0];

现在我怎样才能使顶角变圆。

【问题讨论】:

  • 你为什么不使用和图像而不是图纸?
  • 抱歉,如果您可以制作一个包含所有所需视觉方面的自定义按钮,为什么还要玩这么多呢?
  • 对不起,我希望通过使用 CALayer 类的属性来实现此功能。

标签: iphone objective-c ios uibutton calayer


【解决方案1】:

要制作圆角,您必须使用渐变层 CAGradientLayer。

【讨论】:

    【解决方案2】:

    别忘了

    #import <QuartzCore/QuartzCore.h>
    

    如果您使用的是 CAGradientLayer

    【讨论】:

      【解决方案3】:

      您可以查看ColoredRoundedButton 开源实现。它使用 UIButton 的 CALayercornerRadius 属性来获得圆角。

      【讨论】:

        猜你喜欢
        • 2016-04-29
        • 2016-09-06
        • 1970-01-01
        • 2012-05-06
        • 1970-01-01
        • 2013-10-14
        • 2020-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多