Custom UIButton iOS App - ios

I'm developing an iOS app and i have to insert an UIButton.
I want to customize this button and i'd like to have a button like this:
My code is this, but i'm not able to make rounded corners:
_topImagesScrollViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
_topImagesScrollViewButton.frame = CGRectMake(screenWidth/2-25, topBarHeight+paddHeight, 37 , 37);
_topImagesScrollViewButton.backgroundColor = [UIColor colorWithRed:red_middle green:green_middle blue:blue_middle alpha:alpha_middle];
[_topImagesScrollViewButton setTitle:#"Top" forState:UIControlStateNormal];
[_topImagesScrollViewButton setTitleColor:[UIColor colorWithRed:red_text green:green_text blue:blue_text alpha:alpha_text] forState:UIControlStateNormal];forState:UIControlStateNormal];
[_topImagesScrollViewButton addTarget:self action:#selector(topImagesScrollVieButtonTapped) forControlEvents:UIControlEventTouchUpInside];
How can i do?

I would use the above image to set the image of the button, instead of drawing the shape:
[_topImagesScrollViewButton setImage:#"yourimagename.png" forState:UIControlStateNormal];

This is how to make UIView with rounded corners: IOS: create a UIImage or UIImageView with rounded corners
And what about text color... everything seems to be ok. Maybe it doesn't work because you set title color twice for the same state. And the second from the bottom string of your code is excess.

Related

Blue image for buttons

I have buttons in my costoum cell , and i'm setting the images from code . But something strange is happening. My images are blue .
BOOL isTheObjectThere = [self.favoriteArry containsObject:self.tableData[indexPath.row]];
if (isTheObjectThere==TRUE) {
cell.favBtn.hidden = NO;
[cell.favBtn setImage:[UIImage imageNamed:#"favorite_star.png"] forState:UIControlStateNormal];
cell.favBtn.tag = indexPath.row;
[cell.favBtn addTarget:self action:#selector(unfavoriteBtn:) forControlEvents:UIControlEventTouchUpInside];
As mentioned in the comments, you should create the button with a UIButtonTypeCustom type in case it's currently set to UIButtonTypeSystem and you want to avoid the tint color from taking over. Alternatively, you can set the image rendering mode to make sure you always get the original image and not a tinted one:
[[UIImage imageNamed:#"favorite_star.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

Rounded UIButton-cant align text

I have a UIButton in a circle shape(corner radius=width/2) , and i am trying to center the title of that button , to be in the center of the circle . somehow its not on the center, and i have tried everything ,with no success.
I can see the + sign(its title) a little bit left and up from the center.
With letters its the same-the letter title is not centered :
UIButton *menu = [UIButton buttonWithType:UIButtonTypeCustom];
menu.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
[menu setTitle:#"M" forState:UIControlStateNormal];
[menu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
menu.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
menu.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter ;
menu.frame=CGRectMake(delta, 0, mwidth, mwidth);
menu.layer.cornerRadius=menu.frame.size.width/2.0;
menu.layer.borderWidth=1.0;
Have also tried to set the titleLabel alignment to center without success .
You can try to adopt position of the title by playing with setTitleEdgeInsets method and try with different insets and choose which fits best your customisation

Restrict UIButton titleText frame

I have a custom UIButton that has some text I am setting on it dynamically.
The problem:
If the text gets too large, it will cover up a white arrow that is on the button's image located on the far right here:
When that text gets too large, that white arrow is covered, which I need to avoid.
Example:
Current code:
[self.filterButton setTitle:#"All" forState:UIControlStateNormal];
self.filterButton.titleLabel.adjustsFontSizeToFitWidth = YES;
self.filterButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.filterButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[self.filterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
Some approaches I have tried:
Setting the titleText frame to be %0.85 of the buttons frame.
Current code
Tried to code it to where the frame cuts off at a certain point (of the titleText)
Thanks for any guidance
Have you tried setting the titleEdgeInsets rather than the contentEdgeInsets?

UIButton with transparent image background not drawing transparent

I have run into a strange issue. I have a UIButton with UIButtonTypeCustom.
For it's background image, I am using a transparent image. The issue is that the transparency on the actual image doesn't seem to be correct. The odd thing is that it is in fact transparent, because the background shows correctly behind the button.
Below is an example of what the button looks like (left) and what the button should look like (right). I took a screenshot and overlaid the image on the background in Photoshop, and the background shows correctly inside the image, while in the actual button on the left it does not. Noticeably, the glow is more intense on the left UIButton vs. the actual image when inserted onto the background.
Here's the image I am using to show that it does in fact have transparency:
Here's my code:
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
nextButton.backgroundColor = [UIColor clearColor];
nextButton.frame = CGRectMake(0, 0, 30, 30);
[nextButton setBackgroundImage:[[UIImage imageNamed:#"ButtonBackground.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5] forState:UIControlStateNormal];
forState:UIControlStateHighlighted];
[self addSubview:nextButton];
I have used the exact same image elsewhere to draw with and had no issue with transparency.
UPDATE: Adding other transparent images similarly increases the intensity of the alpha. While they're transparent, they seem darker and therefore less transparent. Again, works perfect elsewhere.
UPDATE 2: Even worse, I just created a new project with the exact same image dragged from the other project, created a button and had no issues with the button displaying correctly. How incredibly annoying!
You shouldn't have to set the background color for it to be transparent. Also, try removing the stretchable call on the image.
Also, you should be setting the image, not the background image.
If none of that helps, then Apple may just be improperly rendering your image. Try creating a CALayer and set its contents to your image and see if that works properly.
try this
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
nextButton.frame = CGRectMake(0, 0, 30, 30);
nextButton.alpha=0.5f;
[nextButton setBackgroundColor:[UIColor clearColor]];
[nextButton setOpaque:NO];
[nextButton setBackgroundImage:[UIImage imageNamed:#"ButtonBackground.png"]
forState:UIControlStateNormal];

UIButton wierdness: Unable to set title for a custom button type

I'm trying to set the title for a custom button that I've created programmatically. The button's image and the frame come up fine, but the title doesn't. I can't really think of anything wrong with this code, so any help is apreciated!
self.helpButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.helpButton setFrame:CGRectMake(113.0, 685.5, 73.0, 40.0)];
UIImage *helpImg = [UIImage imageNamed:#"11_HelpCancel_Up.png"];
[self.helpButton setImage:helpImg
forState:UIControlStateNormal];
[self.helpButton setTitle:#"Help" forState:UIControlStateNormal];
[self.helpButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// [self.helpButton setFont:[UIFont boldSystemFontOfSize:14.0]];
[self.view addSubview:self.helpButton];
Thanks,
Teja.
Use
[self.helpButton setBackgroundImage:helpImg forState:UIControlStateNormal];
- setImage:forState: seems to override - setTitle:forState:
Was having trouble with the button title showing up on my storyboard when I switched the type of button from system -> custom. The problem was the color of the text changed to white when I made the change so anyone having a similar issue, make sure to check the color of the button text.

Resources