开发者

How to get black glossy UIButton look?

开发者 https://www.devze.com 2023-04-05 19:27 出处:网络
Is this possible? I\'ve seen it in apps but I don\'t want to resort to using an image file to achieve开发者_开发知识库 it.You can use this code:

Is this possible? I've seen it in apps but I don't want to resort to using an image file to achieve开发者_开发知识库 it.


You can use this code:

-(void) addGradient:(UIButton *) _button {

    // Add Border
    CALayer *layer = _button.layer;
    layer.cornerRadius = 8.0f;
    layer.masksToBounds = YES;
    layer.borderWidth = 1.0f;
    layer.borderColor = [UIColor colorWithWhite:0.5f alpha:0.2f].CGColor;

    // Add Shine
    CAGradientLayer *shineLayer = [CAGradientLayer layer];
    shineLayer.frame = layer.bounds;
    shineLayer.colors = [NSArray arrayWithObjects:
                         (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                         nil];
    shineLayer.locations = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.8f],
                            [NSNumber numberWithFloat:1.0f],
                            nil];
    [layer addSublayer:shineLayer];
}


"Resorting" to an image file is the right thing to do. Sure, you could write code to make gradients and stuff, but it's much easier done in photoshop.

Set the background image of the button to a stretchable image, and be done with it.


I've faced a particular issue when using these glossy UIButton classes in iOS 6. While a shadow and a thick border would show up, the actual UIButton's background would come up as white.

Turns out that if you enter your code in viewDidLoad, under autolayout, your created views will not yet have their frame, so your layer ends up with a frame of CGRectZero.

Simply move your code, ( most importantly the part where you set the frame of the gradient layer) to viewDidLayoutSubviews.


Use a couple of layers with different gradients.

Have a search around Google - this is perfectly doable in code.


Try this: http://www.mlsite.net/blog/?p=1837, it's a category class for UIButton that makes it glossy. It's a little yellow for my taste, but easier than trying to maintain a whole bunch of Photoshop buttons.


This custom UIButton component may help.

How to get black glossy UIButton look?


I have modified a bit code of @julie and swift4 version

override func viewDidLayoutSubviews() {

     addGradient(btn_near);
    addGradient(btn_location);
    addGradient(btn_addramp);
}


func addGradient(_ _button: UIButton?) {

    // Add Border
    let layer: CALayer? = _button?.layer
    layer?.cornerRadius = 8.0
    layer?.masksToBounds = true
    layer?.borderWidth = 1.0
    layer?.borderColor = UIColor(white: 0.5, alpha: 0.2).cgColor

    // Add Shine
    let shineLayer = CAGradientLayer()
    shineLayer.frame = layer?.bounds ?? CGRect.zero
    shineLayer.colors = [UIColor(white: 1.0, alpha: 0.4).cgColor, UIColor(white: 1.0, alpha: 0.2).cgColor, UIColor(white: 0.75, alpha: 0.2).cgColor, UIColor(white: 0.4, alpha: 0.2).cgColor, UIColor(white: 1.0, alpha: 0.4).cgColor]
    shineLayer.locations = [NSNumber(value: 0.0), NSNumber(value: 0.5), NSNumber(value: 0.5), NSNumber(value: 1.0), NSNumber(value: 1.0)]
    layer?.addSublayer(shineLayer)



}
0

精彩评论

暂无评论...
验证码 换一张
取 消