Changing Button Text - Xcode 6 - ios

I'm currently developing my first app, and I'm having a bit of an issue. I've made a custom button using JavaScript, then by screenshotting I've been able to import it as a custom button into XCode. However, I'm unable to put any text on it; anything I do appears to the side. I've searched through other threads and found the following code:
[_Button setTitle:#"ClickMe!" forState:UIControlStateNormal];
However, this just causes text to appear to the side of the custom button. Can anyone help me fix this?
EDIT:
Here's my code in ViewController.h:
#property (strong, nonatomic) IBOutlet UIButton * RoadMap;
#property (weak, nonatomic) IBOutlet UILabel * myLabel;
And.. relevant code in ViewController.m
_myLabel.text=#"Road Map";
[_RoadMap removeFromSuperView];
[_RoadMap setBackgroundImage:[UIImage imageNamed:#"button.PNG"] forState:UIControlStateNormal];

Looks like you using foreground image for button.
Try to use background image:
[_Button setBackgroundImage:[UIImage imageNamed:#"imageName"] forState:UIControlStateNormal];
In this case text will rendered above the image.
Update:
Just remove these two lines of code:
_myLabel.text=#"Road Map";
[_RoadMap removeFromSuperView];
And add this line:
[_RoadMap setTitle:#"RoadMap" forState:UIControlStateNormal];

Related

UIButton in UITableViewCell set background color not working

I have custom UITableViewCell like this :
#property (weak) IBOutlet UIDefaultServerButton * defaultButton;
#property (weak) IBOutlet UILabel * nameLabel;
#property (weak) IBOutlet UIRoundButton * deleteButton;
#property (weak) IBOutlet NSLayoutConstraint * buttonHeight;
#property (weak) IBOutlet NSLayoutConstraint * buttonWidth;
#property id <TableViewCellDelegate> delegate;
- (void) setDefaultServer: (BOOL) def;
- (void) animateLock;
- (void) animateTrash;
In the cellForIndex.. I add LongPress recogniser. When long press I call function animateLock.
- (void) animateLock
{
[self.deleteButton setImage:[UIImage imageNamed:#"lock"] forState:UIControlStateNormal];
self.deleteButton.hidden = NO;
self.deleteButton.backgroundColor = [UIColor redColor];
}
The weird thing is that button become visible and image has been change, but backgroundColor is ignored (no matter which color I set).
Why ?
Probably the image you have taken is of the same size as that of your button. Either make your image smaller or increase the area of your button.
The code which you are using is perfectly fine, but the reason of background colors is not visible, is that background image is hiding the background color of UIButton. You can confirm this by commenting the line
[self.deleteButton setImage:[UIImage imageNamed:#"lock"] forState:UIControlStateNormal];
And then button's background will be visible.
If you can elaborate that exactly why you want to set red background of button than i will look into that.

Displaying UIButton programmatically

I'm trying to display a button to the view programmatically with this code:
- (void)viewDidLoad {
_detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
_detailButton.frame = CGRectMake(23, 294, 72, 37);
[_detailButton setTitle:#"UIButton!" forState:UIControlStateNormal];
[_detailButton addTarget:self
action:#selector(showPop:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_detailButton];
}
I have the button declared in the .h file like this:
#property (strong, nonatomic) UIButton *detailButton;
The code does not throw any errors or warnings, but when I run there is no button displayed.
I check your code in my project its working fine i think in your case there are some other view that cover this button so check that your code has no problem at all.
Your button probably covered by another view. Just make sure its on top of other views.
In viewWillAppear add the following line of code:
[self.view bringSubviewToFront:self.detailButton];
If your button's type is of UIButtonTypeCustom, the button's default title color is white, so you can't see it if your view's background color is white.
[_detailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

How to set Background image of button

I have a question and I didn't find an answer on it yet. I would like to know how to set the background image of a button in your code. I would like to use a value to change the background.
For example: if (value == one) { set background of button code}
I have two backgrounds but when you give a code you can use buttonimage1.png, buttonimage2.png.
Thnx
First you should add IBOutlet for your button, example:
#property(nonatomic, weak) IBOutlet UIButton *bttn;
next, using this outlet you can change background:
if (value == one) {
[self.bttn setBackgroundImage:[UIImage imageNamed:#"buttonimage1.png"]
forState:UIControlStateNormal];
} else {
[self.bttn setBackgroundImage:[UIImage imageNamed:#"buttonimage2.png"]
forState:UIControlStateNormal];
}
that's all

Styling a button that was created in interface builder

If you have a button that was created in Interface Builder and already points to another viewController how do you target that button in code to give it a custom background for normal and selected state in code?
UIImage *registerButtonNormal = [[UIImage imageNamed:#"yellowRegisterButton"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[self.registerButton setBackgroundImage:registerButtonNormal forState:UIControlStateNormal];
UIImage *registerButtonSelected = [[UIImage imageNamed:#"yellowRegisterButtonSelected"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[self.registerButton setBackgroundImage:registerButtonSelected forState:UIControlStateSelected];
This is actually really easy.
You just have to create a #property and connect the button you want to style by control dragging from the button to the newly created property.
The property should look something like this:
#property (nonatomic, strong) IBOutlet UIButton *registerButton;
Then in the viewDidLoad, you put the above code that targets your button:
- (void)viewDidLoad
{
[super viewDidLoad];
// code for styling button goes here.
}
You can see the code for styling the button in the question.

UIButton image only changes for one button

I have written some code which toggles 2 buttons depending on which one is selected. If the UK one is selected it becomes ticked and the BR one becomes unticked, and vice versa. However, this only seems to be the case for the UK button. If I select the BR button than the UK button unticks, the BR button briefly ticks but then it unticks again.
I have linked up my buttons correctly (I have triple checked), and as the BR button briefly ticks it is definitely linked up. The code I am using is below:
.h
#property (weak) IBOutlet UIButton *btUK;
#property (weak) IBOutlet UIButton *btBR;
.m
- (IBAction)changePortal:(id)sender
{
UIButton *button = (UIButton *)sender;
if (button.tag == kUKButton)
{
self.btUK.imageView.image = [UIImage imageNamed:#"tick_box.png"];
self.btBR.imageView.image = [UIImage imageNamed:#"tick_box_empty.png"];
[Singleton sharedSingleton].bUseUKPortal = YES;
}
else if (button.tag == kBRButton)
{
self.btBR.imageView.image = [UIImage imageNamed:#"tick_box.png"];
self.btUK.imageView.image = [UIImage imageNamed:#"tick_box_empty.png"];
[Singleton sharedSingleton].bUseUKPortal = NO;
}
}
I have set break points within the code and have confirmed that both the buttons go in to their relevant sections when clicked. I can also confirm that no other code is using the btUK and btBR variables as I have just written it all.
Both buttons have changePortal set as their action, and the function is only called once per click.
I have also tried cleaning the code but this did not fix my issue.
If anyone can shed any light as to why this is happening then I would be very grateful.
The correct way to set the image of a UIButton is to call setImage:forState:.
So try to alter your code to something like this:
// Follow this pattern for every button image change
[self.btUK setImage:[UIImage imageNamed:#"tick_box.png"] forState:UIControlStateNormal];
Now regarding the imageView property the documentation states that:
The button’s image view. (read-only)
#property(nonatomic, readonly, retain) UIImageView *imageView
Discussion
Although this property is read-only, its own properties are
read/write. Use these properties to configure the appearance and
behavior of the button’s view. For example:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.imageView.exclusiveTouch = YES;
The imageView property returns a value even if the button has not been
displayed yet. The value of the property is nil for system buttons.

Resources