How to change custom button's text? - ios

I need to set text to the this custom button.I thought it would be as easy as using setText method to set its text. But, unfortunately, it doesn't work.
So, why this approach doesn't work for any custom buttons?
When we changed its type to custom, basically how it effects this UIButton?
Thank you.
EDIT: I've created this UIButton through IB. The code used for changing text is this :
[self.btnDiscover setTitle:#"Discover" forState:UIControlStateNormal];

Use setTitle:forState: instead of setText, e.g.:
[button setTitle:NSLocalizedString(#"CONTACT_BUTTON", #"Contact Us") forState:UIControlStateNormal];
Also, make sure you are using setBackgroundImage:forState: to set the button image and not setImage:forState:.
As to your question about the custom style, assigning UIButtonTypeCustom to a button simply means that the button is assigned no style (source):
UIButtonTypeCustom
No button style.

Related

UITableView button image disappears while scrolling

I have a UITableView (like FaceBook post where I want to like, share, comment). When I click one like button it should turn to some other image and also it should call a method for further process.
I am using Objective-C
In the Storyboard, just change the Button properties for different State Configurations according to your requirement.
According to your requirement, you need to set the different Image property for both Default and Selected states.
No need to write any extra lines of code for that.
You can do with set image for State Config of button [Normal, Highlighted, Selected, Disable] at design.
When you click on the button, you call a method and change state of button Selected or None.
Seem like it's because of dequeueReusableCellWithIdentifier method. In cellForRowAtIndexpath, you need to check button is clicked in this cell or not.
If button in this cell is clicked, set image of button with clicked image. If not, set image of button with normal image.
You can set images for different states like below
[btn setBackgroundImage:[UIImage imageNamed:#"image1"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"image2"] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImage imageNamed:#"image3"] forState:UIControlStateHighlightedSelected];

CALayer change borderColor on UIControlState change

I have a customized button that has a transparent background and a custom border set in the button's layer.
I want to change the border color of the button to a darker color when the button is pressed, e.g. during UIControlStateSelected, but I can't find an easy way to do this.
Is this possible? Or do I need to find a workaround for this?
You can listen to an event like UIControlEventTouchDown and configure the border when it occurs. You need to add an observer as follows:
[yourButton addTarget:self
action:#selector(configureBorderIn)
forControlEvents:UIControlEventTouchDown];
You should add an observer to configure the border when the button is released, for example detecting UIControlEventTouchUpInside. You have a list of all events related to buttons here: UIControl class reference.
Hope it helps!
have a good day.

How to make a UIButton invisible but also still work if pressed?

I have a situation in which I need to have a button added to my view using Interface Builder, but the button shouldn't be seen, but if the user clicks the location of the button, the button can still call its method.
I've tried setting the opacity to zero, or changing the button to 'hidden', but both of these strategies result in a button that is completely invisible, but also useless, as it can't be pressed.
Does anyone have a solution for this?
You can try to custom type UIButton:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
With empty title, empty background image.
Or set the type from the storyboard or Interface Builder:

How to set the Button text programmatically in iOS?

I know how to change the button text programmatically in iOS:
[myButton setTitle:#"myTitle" forState:UIControlStateNormal];
but my need is I have a label called _lblgenisis, I put some code to change the text of this label and it works fine for me, the code for that is:
_lblGenisis.text = [NSString stringWithFormat:#"%# %#",delegate.selectedBook,delegate.selectedChapter];
my requirement is I have to convert to this code to show the same in button instead of label my button name is _btnGenisis. How to do this?
Solution
Answer that works for me:
NSString *myTitle = [NSString stringWithFormat:#"%# %#",delegate.selectedBook,delegate.selectedChapter];
[nextButton setTitle:myTitle forState:UIControlStateNormal];
Try to add the label to the button?
[_btnGenisis addSubview:_lblGenisis]
Let me see if I get this right.
The code that normally works for changing your button label is not working to set it programmatically?
I have had this issue it turns out that:
When you set the label in the xib file you can see the results and won't make any mistakes. Recently I had a custom button with an image and couldn't figure out why I could not add a title programmatically.
It turns out I was setting the title properly (the way you already know how to). The real issue was that I set my button's image with setImage:forState. This image is above the button and you should check your xib file to see if you have an image set for the button. If so, delete the image and set it as the background image.
To see if this is actually the issue, try this in your code where you are trying to set the title
NSLog(#"image for button %#", _btnGenisis.currentImage);
It should output (null)
If it outputs like you have an image covering your button's title.
Check the interface file and inspect the button. Make sure there is no default image set. You can set a default background image.
If it is all created in code do a search for
[_btnGenisis setImage:
You should not be setting the image if you want a title. Use
[_btnGenesis setBackgroundImage:yourTitleHere forState:UIControlStateNormal];

Round UIButton

I want to know whether drawing a round UIButton(not rounded rect) is possible.
When I add a round image in a UIButton of type custom, then it looks like a round button. But at the moment the button is clicked the boundary of the button becomes visible, so that it looks like a square button, then again when the click ends it looks like a round button.
I want the button to look like a round button even at the moment the click happens. is this possible?
Tested Code:
.h
-(void)vijayWithApple;
.m
-(void)vijayWithApple{
NSLog(#"vijayWithApple Called");
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Micky.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
Result
Just create two rounded button images, one for the pressed state and one for unpressed.
In IB, create a UIButton of type Custom. Go to the section where you set a background image, and set the dropdown with "All" to normal - now set the image to your unpressed image.
Then change the dropdown to "Selected", and put in your pressed image.
Now change the fill type to be aspect fit, and make the button sqare. Use as a normal UIButton anywhere. You can of course also easily do this all programaitcally with similar steps (setting images for UIControlStateNormal and UIControlStateSelected).
You'd probably have to create a custom control for that, but do you really need a round button?
Every platform has its UI conventions, and the iPhone is no exception. Users expect the buttons to be rounded rectangles.
UPDATE in response to comment:
If I'm getting this right, you're not looking for a round button, but rather a clickable (touchable) image.
You can use an UIImageView and its touchesBegan method.
UPDATE 2:
Wait a second. What kind of radio button are we talking about? For some reason I thought you were trying to imitate a real radio. If you're talking about a radio button group, please use a UISegmentedControl or a UIPicker.
UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
but.layer.cornerRadius=50;
From what I know of the SDK (admittedly I have very little experience beyond tutorials in books/screencasts), you'd have to create your own control to have a radio button. There are a couple of suggestions on how to implement that in this question.
That said, from a user's perspective I feel like it would be better to stick to the native controls. iPhone users are used to specific controls (i.e. UITableViews or UIPickerViews) for this type of interaction. Straying from practically universal UI conventions tends to make the user experience more jarring.
take the help of this site for creating Round Button on iPhone.
You can get many custom controls :)
http://www.cocoacontrols.com/controls
http://www.cocoacontrols.com/platforms/ios/controls/uiglossybutton
H#PPY CODING !
Thanks & regards,
Gautam Tiwari

Resources