There's a shadow on my button - iphone

i'm creating a button programmaticly for an iPad application. when i see the button, there looks to be a shadow type thing below it. what is it and how can i get rid of it?
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 i only get that effect when its in my scrollview. if i add it to the view, no shadow effect.
the top test button, the button is a subview of the view. the bottom button is a subview of the scrollview which is a subview of the view (button/view vs button/scrollview/view).
white section is the view, grey is the scrollview/view.
UPDATE 2:
as pointed out by robmayor, UIButtons always have that double line effect, just not noticeble when the background color is white. the blue is a view and the grey is the subview scrollview.

This question is old (6 months) but i'have found a solution to delete/mask this bad effect of double lines.
[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];
UIColor selected is depending of the current background of your view.
Result :

On iPads, a rounded-rect UIButton always draws a white line along its bottom edge. You can't see that white line if the button's superview is white, but it's still there.
You have a few options:
Make the superview white. This is the easiest but you might not like the way it looks.
Make some rounded rect images in your favorite image editor. Set the button type to custom and set your rounded rect images as the button's images.
Make a subclass of UIButton and override its drawRect: method.
Set the button type to custom and use the button's layer properties (button.layer.backgroundColor, button.layer.borderColor, button.layer.borderWidth, button.layer.cornerRadius) to give the button a rounded rect appearance. You'll have to update button.layer.backgroundColor when the button is touched if you want it to turn blue like a normal one does. (Actually a normal one uses a blue gradient.)

Replace [UIButton buttonWithType:UIButtonTypeRoundedRect] with:
UIButton *myButton = [UIButton new];
or:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
You want to have your customized button. You can still make it with rounded corners if needed.

Related

Resizing UIButton on 3.5inch screen

I have two UIButtons in my app, which are custom buttons with background images, so they look like two dice. When my app is run on something not-iPhone 5 (not 4.0 inch screen) I want the to resize the buttons. I already have the code that checks for non-iPhone 5 devices(and it works), I just need to know how to change the size of the UIButtons.
I have tried lots of answers for similar questions, but so far I have not written any code that actually changes the size of my UIButtons.
I have written something like this in the "viewDidLoad" inside my "check-if-screen-is-non-iPhone-5"-statement:
button.frame = CGRectMake(90, 200, 100, 100);
The answer will depend on whether you're using Interface Builder or creating yoru button programatically. Most likely, you want to set the "content mode" of the button's imageView; then update the button's frame.
Here is an example:
UIImage *image = [UIImage imageNamed:#"close"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height);
button.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[button.imageView setContentMode:UIViewContentModeScaleAspectFit];
[button setImage:image forState:UIControlStateNormal];
Good luck.

iOS UIButton image contentMode flickering on TouchUpInside

I have a problem with UIButton image if I set its imageView contentMode to UIViewContentModeScaleAspectFill.
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
imageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[imageButton setImage:[UIImage imageNamed:#"myImage.jpg"] forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(doSmth:) forControlEvents:UIControlEventTouchUpInside];
The image inside button is properly scaled to fill the whole button area.
After a click/touch on button the image gets resized (flickers) like if it's contentMode is set to UIViewContentModeScaleAspectFit.
Does anyone know how to remove this flickering when click/touch occurs?
Thanks!
Why are you setting content mode of the button's imageView? The image, you set for specified state, is always filling the size of the button. So you have to set button's frame size to the size of the image and not to set imageView contentMode
The flickering could be the higlighting of the button. If you want to set custom image for highlighting, use this code:
[imageButton setImage:buttonHightlightImage forState:UIControlStateHightlighted]
Just disable/uncheck the highlighted adjusts image in attributes inspector for imageButton

UIButton with Custom border colour, iPhone

I want to create Custom UIButton with rectangular shape. for this I am using rectangular view as a Background for UIButton and making UIbuttons background colour as clearcolor. But border of UIbutton still remains. any suggestions on how to make border disappear?
Thanks for any help in advance..
Try this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button.layer setBorderColor:[[UIColor clearColor] CGColor]];
but don't forget to add in your .h or .m file
#import <QuartzCore/QuartzCore.h>
I wasted a few minutes and got frustrated that the border was not showing up even though I was setting the layer property as suggested above.
When I set the width using the following, I saw the button border.
[[button layer] setBorderWidth:2.0f];
button.layer.borderColor = UIColor.orangeColor().CGColor
swift example for orange border
You can set the border properties on the CALayer by accessing the layer property of the button.
Add Quartz
#import <QuartzCore/QuartzCore.h>
Set properties of the button:
[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor AnyColor].CGColor];
You should set type of your UIButton to Custom, not Rounded Rect (if you are creating button via IB).
If you are creating UIButton programmatically then you this code:
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
And after that you can make your own customizations.

Rounding color around borders of a button

I've got a little problem as seen below:
The cell has a background color, and the button doesn't. Still, it doesn't give me rounded edges, but corners. How can I fix that?
UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[[meerKnop layer] setCornerRadius:8.0f];
meerKnop.backgroundColor = [UIColor whiteColor];
meerKnop.frame = CGRectMake(11.0, (60.0 + (teller * 52.5)), 299.0, 50.0);
...
[meerKnop addSubview:locationLabel];
...
[meerKnop addSubview:categoryLabel];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[meerKnop addGestureRecognizer:swipe];
[swipe release];
[meerKnop addTarget:self action:#selector(alertPressed:) forControlEvents:UIControlEventTouchUpInside];
meerKnop.tag = incId;
[cell addSubview:meerKnop];
Try setting the corner radius of the layer of the button.
[button.layer setCornerRadius:10];
Remember to import if you are using layer property
Also, use:
[[button layer] setMasksToBounds:YES];
With this code the layer gets a corner radius of 10.0 and the -setMasksToBounds: tells the button’s layer to mask any layer content that comes below it in the layer tree. This is necessary in order for the layer to mask off the rounded corners.
UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Change this to:
UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeCustom];
Edited:
UIButton *meerKnop = [UIButton buttonWithType:UIButtonTypeCustom];
[[meerKnop layer] setCornerRadius:8.0f];
meerKnop.backgroundColor = [UIColor redcolor];
meerKnop.frame = CGRectMake(11.0, (60.0 + (teller * 52.5)), 299.0, 50.0);
show me where the big white rectangle is appearing? (I hope u have cleared the cell background color).
change the background colour of the button to same as that of cell.. it will take care of the rounded edges as there colour will become same as that of the background. sry i new.. i can think of this way only at the moment.. Hope it helps..

UIButton not firing selector if I draw it using CALayer

Hi everybody I create this button like this:
_myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
DLBadgeLayerDelegate *layerDelegate = [[DLBadgeLayerDelegate alloc] init];
_myButton.frame = CGRectMake(0.0 , 0.0, 100.0, 100.0);
[_myButton addTarget:self action:#selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
_myButton.layer.backgroundColor = [[UIColor redColor] CGColor];
_myButton.center = CGPointMake(10.0, 10.0);
[self addSubview:_myButton];
[_myButton release];
The button appears on the screen but if I tap on it nothing happens. If I just create a Rounded rect button instead with no custom drawing on its layer or if I just leave the layer line of code out all works well. Do you have any Idea why do I get this behaviour as I would like to do some custom drawing in the button's layer?
Events are not sent to a control that is (1) Hidden, (2) alpha < 0.1, (3) userInteraction=NO, etc. Since you have no button image, no button title, no background color, no background image, maybe it is being treated as transparent (like alpha < 0.1) and is not getting events. Try adding an image, title, or background color (in the UIView, not the layer) to see if that's the problem.
EDIT: Try putting the background color in the view's backgroundColor instead of in the layer's backgroundColor:
_myButton.backgroundColor = [UIColor redColor];
If I leave this line out:
_myButton.layer.backgroundColor = [[UIColor redColor] CGColor];
the event is triggered correctly and I still have no title, alpha, etc. I don't think that is the problem or explication, but I will try that way too.