Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton半透明边框_Ios_Cocoa Touch_Uibutton_Uikit - Fatal编程技术网

Ios UIButton半透明边框

Ios UIButton半透明边框,ios,cocoa-touch,uibutton,uikit,Ios,Cocoa Touch,Uibutton,Uikit,我有一个自定义按钮,边框应该是半透明的白色 如果我这样做: - (void) awakeFromNib { self.layer.cornerRadius = 6.0f; self.layer.borderWidth = 4.0f; self.layer.borderColor = [[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor]; } 我得到了这个-透明度,但按钮的原始颜色: 边框是半透明的,但按钮

我有一个自定义按钮,边框应该是半透明的白色

如果我这样做:

- (void) awakeFromNib {        
    self.layer.cornerRadius = 6.0f;
    self.layer.borderWidth = 4.0f;
    self.layer.borderColor = [[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor];
}
我得到了这个-透明度,但按钮的原始颜色:


边框是半透明的,但按钮的颜色不同。

您是否尝试过以下方法:

self.backgroundColor = [[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor];
更简单的方法是通过选择按钮,然后在属性检查器中选择alpha和背景色,在Interface Builder中设置颜色:


将子层的颜色设置为您希望按钮的颜色(不要设置按钮本身的背景色),并相对于按钮的rect插入其rect

- (void) awakeFromNib {
    self.layer.cornerRadius = 6.0f;
    self.layer.borderWidth = 4.0f;
    self.layer.borderColor = [[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor];
    CALayer *sub = [CALayer new];
    sub.frame = CGRectInset(self.bounds, 4, 4);
    sub.backgroundColor = [UIColor redColor].CGColor;
    [self.layer addSublayer:sub];
}

另一种方法是为self.layer和子层使用背景色,如果希望背景色也被舍入,这种方法会更好。在这种情况下,根本不需要使用边界

- (void) awakeFromNib {
    self.layer.cornerRadius = 6.0f;
    self.tintColor = [UIColor whiteColor]; // make white text
    self.layer.backgroundColor = [[UIColor colorWithWhite:1.0f alpha:0.4] CGColor];
    CALayer *sub = [CALayer new];
    sub.cornerRadius = 4.0f;
    sub.frame = CGRectInset(self.bounds, 4, 4);
    sub.backgroundColor = [UIColor blueColor].CGColor;
    [self.layer addSublayer:sub];
}

以防有人寻找外部边界透明的UIImage。若你们只是设置一个图层边框,你们会得到一个透明的边框,但你们会看到那个边框后面的内部图像,而不是外部图像。我设法创建了一个外部边界透明的ImageView

这个想法很简单。我从UIImageView保存UIImage。然后我删除UIImage并将初始层设置为边界层。然后我在上面放了一个新的较小的子层,并将保存的UIImage设置为它的内容

#import <UIKit/UIKit.h>

@interface SSCircleImageView : UIImageView


@end
看起来是这样的:

#import "SSCircleImageView.h"



@implementation SSCircleImageView


const CGFloat borderWidth = 5.0f;
const CGFloat borderAlpha = 0.3f;


- (void)awakeFromNib
{
    UIImage *image = self.image;
    self.image = nil;

    self.clipsToBounds = YES;
    self.layer.cornerRadius = self.frame.size.width / 2.0;
    self.layer.borderWidth = borderWidth;
    self.layer.borderColor = [[UIColor colorWithWhite:1.0f alpha:borderAlpha] CGColor];

    CALayer *subLayer = [CALayer layer];
    subLayer.frame =  CGRectInset(self.bounds, self.layer.borderWidth, self.layer.borderWidth);
    subLayer.cornerRadius = subLayer.frame.size.width / 2.0;
    subLayer.contents = (id)image.CGImage;
    [self.layer addSublayer:subLayer];
}



@end