UIButton Rounded Rect unwanted 3D effect

余生长醉 提交于 2020-01-01 19:53:08

问题


I had a couple of UIButtons on a nib of type RoundedRect which before iOS 6 looked nice and flat. In iOS 6 though they've acquired a 3D effect border which I cannot get rid of (I'd supply a pic but my reputation is too puny). Can anybody suggest how to get the nice flat look back?

I'm sure this will turn out to be the stupidest question I've ever asked and that the answer will be so simple I'll kick myself for a week. But I can't find it and any help would be much appreciated.


回答1:


I don't know why that effect appeared , but one way to do this is to avoid Rounded Rect. I always use custom buttons. You can customize them in code in any way you want .

For example:

button.layer.cornerRadius = 8;  //this gives it rounded corners
button.layer.borderColor = [[UIColor redColor] CGColor] ; // this is how you set a border
button.layer.borderWidth = 2; //this is how you set the width of the border

The layer member of the UIButton is a CALayer and you can set shadows to it . This is a good tutorial.

You can also add a gradient background in order to make the button nicer , like this:

CAGradientLayer *layer = [CAGradientLayer layer];

layer.colors = [NSArray arrayWithObjects:
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.9] CGColor],
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.8] CGColor],
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.7] CGColor],
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.6] CGColor],
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.5] CGColor],
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.4] CGColor],
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.3] CGColor],
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.2] CGColor],
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.1] CGColor],
                (id)[[UIColor colorWithRed:skinRed green:skinGreen blue:skinBlue alpha:0.0] CGColor],
                nil];

layer.locations = [NSArray arrayWithObjects:
                   [NSNumber numberWithFloat:0.1],
                   [NSNumber numberWithFloat:0.2],
                   [NSNumber numberWithFloat:0.3],
                   [NSNumber numberWithFloat:0.4],
                   [NSNumber numberWithFloat:0.5],
                   [NSNumber numberWithFloat:0.6],
                   [NSNumber numberWithFloat:0.7],
                   [NSNumber numberWithFloat:0.8],
                   [NSNumber numberWithFloat:0.9],
                   [NSNumber numberWithFloat:1.0],
                   nil];

layer.startPoint        = CGPointMake(0, 0);
layer.frame             = button.layer.bounds;

layer.endPoint          = CGPointMake(0, 1);
layer.contentsGravity   = kCAGravityResize;
[button.layer insertSublayer:layer below:button.titleLabel.layer];

button.layer.masksToBounds = TRUE;

As you can see , a nice touch would be to have some global int vars that define the RGB of the app's skin. This way you can modify it in seconds.

So , by using this way you are imune to the changes that Apple makes over SDK versions. And you can customize it in any way that you want , achieving much more than you could ever get in IB.

Hope this helps.

Cheers!



来源:https://stackoverflow.com/questions/12726335/uibutton-rounded-rect-unwanted-3d-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!