The following code basically does work, only the titleLabel is not shown when the result is displayed.
buttonRectis just a CGRect containing the buttons frame. I don't think the cause of the problem is there, because the button is displayed at the position he should be at.
[contactButton.titleLabel center] did not help, bringing the subview titleLabel to front didn't either.
UIButton *contactButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
contactButton.frame = buttonRect;
contactButton.titleLabel.text = #"Contact Someone!";
[contactButton addTarget:self action:#selector(showContactWindow) forControlEvents:UIControlEventTouchUpInside];
[self.tabBarController.view addSubview:contactButton];
I really have no idea what could be causing this.
[myButton setTitle:#"Contact Someone!" forState:UIControlStateNormal];
Related
We have a very simple UIButton with two background images, whenever the button is toggled on there is small rectangle shows on the up left corner of the button. This issue occurs only if the button is UIButtonTypeRoundedRect, if I switch to UIButtonTypeCustom it works fine. Does anybody have a clue?
UIButton *mainBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
mainBtn.tag=0;
mainBtn.frame=CGRectMake(128, 8, 60.0, 60.0);
mainBtn.adjustsImageWhenHighlighted=NO;
[mainBtn setBackgroundImage:[UIImage imageNamed:#"main_tabBar_btn_main.png"] forState:UIControlStateNormal];
[mainBtn setBackgroundImage:[UIImage imageNamed:#"main_tabBar_btn_main_selected.png"] forState:UIControlStateSelected];
[tabBarView addSubview:mainBtn];
[mainBtn addTarget:self action:#selector(tabBarBtnTap:) forControlEvents:UIControlEventTouchUpInside];
try this . . . .
take a custom button , and if you want to get rounded corner's then write following code
#import <QuartzCore/QuartzCore.h>
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 10;
Is the only reason you want to use UIButtonTypeRoundedRect in order to get the rounded corners (i.e. circle)? If so, try just using UIButtonTypeCustom, and set mainBtn.layer.cornerRadius to the radius you want.
I want to add a borderless UIButton to my view. Using the interface builder, I do this by dragging a Round Rect Button from the object library. Then, on the attributes inspector, change the Type to Custom and leave the title to "Button." Everything is okay with the interface builder. But, this is not the case with the programmatic approach. Programmatically, this is how I do it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
When I run the app, the button is not displayed. Maybe I'm missing something here but when I change the type to UIButtonTypeRoundRect the button is displayed. But, again, I want the button to be borderless.
Anyway, I can always use the interface builder. However, I want to understand why the programmatic approach doesn't work. So, does anyone know what the problem is?
UIButtonTypeCustom is really, actually a custom button type. There are no meaningful values setup for it by default. If you want to display it, you have to set its background color and/or its title color to a non-transparent one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
// Set visible values
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[someSuperview addSubview:button];
Here i have used your code the Thing you missing is that
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
Hi I was in the same situation.
I do not know why somewhere in my code there was
button.translatesAutoresizingMaskIntoConstraints = NO;
Once I've deleted that line I was able to set my button frame without any issues.
Hope this helps
I have a button that is initialized like that>
self.button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doAction)];
Later, I am attaching a custom background to it, like that>
[self.button setBackgroundImage:[UIImage imageNamed:#"customNavBar_button_right_enabled"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
and puting it onto navbar.
[self.navigationItem setRightBarButtonItem:self.button
animated:NO];
This image has fixed size. I want that button to have fully custom background image with no "intelligent" resizing with UIEdgeInsets.I don't want any resizing. But for some reason, it behaves like its insets would be active and it is resized to have bigger width, with roughly the middle half of picture considerably stretched to provide for the wider button.
Why is this happening? How can I prevent this to happen?
The easiest way to achieve a bar button item with a custom image is by:
Drag a UIButton onto the storyboard.
Set the button Type to Custom and set your image for that button.
Drag the button onto your Toolbar.
The storyboard automatically creates a UIBarButtonItem and adds your UIButton as a subview. Or if you do NOT want to use storyboards you could implement the following:
// Initialize the UIButton
UIImage *buttonImage = [UIImage imageNamed:#"buttonImage.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
// Initialize the UIBarButtonItem
UIBarButtonItem aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
// Set the Target and Action for aButton
[aButton addTarget:self action:#selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
Hope it helps!
EDIT: Do not forget to add the aBarButtonItem to the UIToolbar.
i stared to built an app and i want that all my buttons in my app will look the same.
i write code for one button and i want my other buttons look alike.
here is my button code:
UIImage *butimage = [UIImage imageNamed:#"button1.png"];
UIImage *butimage3 = [UIImage imageNamed:#"button1press.png"];
UIImage *butpress = [butimage3 stretchableImageWithLeftCapWidth:15 topCapHeight:0];
UIImage *butimage2 = [butimage stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[button1 setBackgroundImage:butimage2 forState:UIControlStateNormal];
[button1 setBackgroundImage:butpress forState:UIControlStateHighlighted];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
how can i do it?
thanks
Try this:
[button1 addTarget:self action:#selector(anAction:) forControlEvents:UIControlEventTouchUpInside]
I'm assuming you've created only a button, and assigning it all the images.
If you create more buttons, the code for giving them an action is the above one.
For the anAction: part, create a -(void)anAction:(id)sender (i.e) in order to tell the button to run that specific action.
EDIT
Declare your buttons as IBOutlets, place them in your viewController.xib and link them.
Then declare their properties in viewWillAppear or viewDidLoad (the code you wrote, basically).
Then if you want them to perform some actions, write some -(IBAction)actionForButton:(id)sender and just assign them to the buttons in IB.
I have a UIButton in each of my tableview's cells. I want to glow the uibutton while click action happends. I set an image as its background image using the code
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
The image.png is little bit darker image. Because i want to show a click effect by showing a glowed image.
Now shall i glow the image.png by code while clicking happends? Or should i create a new glowing image(say image_glow.png) with the same dimension and replace the previous one?
Whatever, please explain how to implement this....
The best way is to use multiple images.
You can set different image to different state of a UIButton by using setImage method.
As i see, you have set image only for UIControlStateNormal state, which makes all other states of a button to use the same image.
reference : UIButton States
Hope this helps.