There is a shadow on my button

I am creating a button programmatically for an iPad application. when I see the button, there is a shadow thing under it. what is it and how can i get rid of it?

shadow button

here is the code that creates it:

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; myButton.titleLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:12]; [myButton setTitle:@"test" forState:UIControlStateNormal]; myButton.frame = CGRectMake(0, 0, self.leftScrollView.frame.size.width, 50); UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gear12.png"]]; [myButton addSubview:myImageView]; [self.leftScrollView addSubview:myButton]; 

UPDATE:

ok, I notice that I only get this effect in my scroll. if I add it to the view, no shadow effect.

top test button, the button is a sub-view. the bottom button is the scrollview sub-item, which is the view subview (button / view vs button / scrollview / view).

the white section is the view, the gray is the scrollview / view.

shadow effect 2

UPDATE 2:

as pointed out by robmayor, UIButtons always have this double line effect, just don’t notice when the background color is white. blue is the view and gray is the subview scroll view.

enter image description here

+6
source share
3 answers

On iPads, a rounded UIButton always draws a white line along its bottom edge. You cannot see this white line if the superview button is white, but it is still there.

You have several options:

  • Make the inscription white. This is the easiest, but you may not like the way it looks.

  • Make a few rounded rectangular images in your favorite image editor. Set the button type to custom and set the rounded rectangular images as button images.

  • Subclass UIButton and override its drawRect: method.

  • Set the type of the button to custom and use the properties of the button layer ( button.layer.backgroundColor , button.layer.borderColor , button.layer.borderWidth , button.layer.cornerRadius ) to give the button a rounded rectangle. You will need to update button.layer.backgroundColor when the button touches, if you want it to turn blue, as usual. (Actually normal uses a blue gradient.)

+5
source

This question is old (6 months), but I found a solution to remove / mask this bad double line effect.

 [yourButton.layer setBackgroundColor: [[UIColor blackColor]CGColor]]; [yourButton.layer setBorderWidth:1.0f]; [yourButton.layer setBorderColor:[[UIColor blackColor]CGColor]]; [yourButton.layer setShadowOpacity:0.1f]; [yourButton.layer setCornerRadius:10]; 

The UIColor you select depends on the current background of your view.

Result: enter image description here

+9
source

Replace [UIButton buttonWithType:UIButtonTypeRoundedRect] with:

 UIButton *myButton = [UIButton new]; 

or

 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

You want to have your own custom button. You can also do this with rounded corners, if necessary.

-1
source

Source: https://habr.com/ru/post/902687/


All Articles