Refactor UIButton to UILabel in storyboard - ios

I've a view in my storyboard with a lot of UIButton with constraints.
Is there a way to transform these buttons to labels without removing each button and create a label with constraints?

UIButton actually contains a UILabel for displaying its titleLabel. So You can set the button UserInteractionEnabled: property to NO.
UIButton may have border color. So set the border color as clear color.
Make the UIButton as custom type.
As you need to reconfigure the constrains again if you added the labels, you can go for the above solution.
Reason for couldn't change in IB/Storyboard:
You have dragged the UIButton from the library to your view. Even you try to change the class name to UILabel or its subclass it won't change the class name.
because UILabel is just a UIView type, Where as UIButton is UIControl type.
That's why you couldn't change the UIButton class name to UILabel or Subclass of UILabel in IB.
Updates:
No Guarantee/Not Recommended on upcoming XCode release

Related

How to add Label on Bar Button Item?

I have UIBarButtonItem which is taken using interface builder.
Its outlet is taken as follow :
{
IBOutlet UIBarButtonItem *itemNext, *itemPrevious, *itemSubmit;
}
so in my storyboard it looks like as following image.
Now, my question is how can i add UILabel just before rightBarButtonItem item ?
Note : UIBarButtonItem is not on the navigationController its set on the bottom of the screen as shown in the image.
i am unable to drag and drop the UILabel on UIBarButtonItem anyone can suggest me how can i do that?
UILabel can only be added as a subview to UIToolbar or UINavigationBar.
You can add a UIBarItem. Use setTitleTextAttributes:forState: on UIBarItem to get attributed string properties similar to UILabel.

UIButton Multiple Labels Using UIControlStates

I know with a UIButton, I can add additional UILabels as subviews:
[myButton addSubview: myLabel];
And (at least, with the default title label) I can set its text color when tapped by using:
[myButton setTitleColor:someColor forState:UIControlStateHighlighted]
My question is, how can I implement this functionality for additional UILabels added to the UIButton (if this is possible)?
Subclass UIButton and add your additional labels in there as instance variables. Then override -setHighlighted and -setSelected to adjust the additional labels as desired.
FYI - you call [myButton setTitleColor...], not [myButton.titleLabel setTitleColor...]
It seems my way of going about it isn't easy, but I realized I can just add an action to the UIButton for the event UITouchDown, and change the labels accordingly in the action.
You would have to set myLabels text color, before you added it as a subView.
Otherwise, you'll have to enumerate through the button's subviews and change each of your added label's text colors.
Update:
You can change the button title's font as follows:
myButton.titleLabel!.font = UIFont(name: "...", 10)
You can change the button's title color as follows:
colorsBtn.setTitleColor(UIColor.brownColor(), forState: UIControlState.Highlighted)

How to instantiate a Custom UIButton in UITableViewCell

I have a UITableViewCell and my cells contain a UIButton subclass (CoolButton).
I have picked the code of CoolButton from raywenderlich tutorial
available here
In the IB I have created a subclass of the UITableViewCell and I have configured the class of my button to CoolButton and set the property type to Custom and configured the IBOutlet for the CoolButton to my UITableView subclass.
My problem is when cells are created, they get a UIButton instead of a CoolButton. As a consequence when I'm setting the properties of the CoolButton I get a crash with "unrecognized selector".
Why UIButton is instantiated instead of a CoolButton? How can I get my CoolButton in the table?
Thanks,
Séb.
I'm going to guess you are doing something like:
CoolButton *b = [UIButton ...]
You need to create it using the CoolButton class:
CoolButton *b = [CoolButton ....];
Or, if you used IB, that you created a UIButton but used an IBOutlet of CoolButton. You would need to make the actual class of the button you drag into IB CoolButton, which you can do by selecting the button and tapping (I think) the second to the left top tab in the inspector.

How to set Accessoryview to static UITableViewCell?

I have created a UITableViewController with a UITableView and static UITableViewCells.
How can I change the Accessory View to a custom Image within a UIImageView?
I know how to change the Accessory View generally:
UIImage *accessor = [UIImage imageNamed:#"imageName"];
[somecell setAccessoryView:[[UIImageView alloc] initWithImage: accessor]];
The solution:
Create an IBOutlet for each UITableViewCell which should have a custom accessory view and connect the IBOutlet to the UITableViewCell in the storyboard. After that you can set the accessory view like above.
I found a better solution:
Drag a view (for example a instance of UISwitch) into UITableViewController in storyboard
Selet the cell on which you want to add a custom accessory view. Then open the Connections inspector, drag the accessoryView in section Outlets to the view that you created in step 1.
Now run the app, see a custom accessory view appearing in a static UITableViewCell. Of course you can create another IBOutlet between the UISwitch and controller so that you could get the reference of it, or create an IBAction for receiving action when user change the value of UISwitch.
Add a custom UIButton in the storyboard and set the image you want to it

UILabel can't become first responder and show inputView in UITableView

I tried to create a subclass of UILabel called EditableLabel and implemented canBecomeFirstResponder, isUserInteractionEnabled, both of those two methods return YES, in the meantime, I over-write inputView and inputAccessoryView and make them writable.
My problem is that when I tap on the label, the inputView can't be shown on the screen. Anybody know how to implement the subclass of UILabel view and let the inputView shown?
Thank you very much.
Why not just use a UITextField and set its borderStyle to UITextBorderStyleNone?

Resources