Label over button, how to highlight text on button press in iOS7? - ios

I have a button and multiple labels placed over the button in storyboard. For the button I specified a default and a highlight state. Also for the labels I specified the highlight color in storyboard.
However on button press the font color of the label does not change to white. Am I missing something? I would like to configure this behavior in storyboard, not programmatically. Is this possible? Or do I have to create a custom button?

The problem is that the UILabel does not get the touch events because it simply does not handle touch events by design, it's just for showing text.
You might want to create a subclass of a UIButton but this is not a good idea since it's kind of a cluster class.
Best way to do it is creating a custom button class by subclassing either UIControl or UIView. With the later you could add it in your storyboard by changing their class to one of your button subclasses. In the subclass make your customizations in the initWithCoder: method.
If you decided to choose the UIControl way of doing this. Look at setHighlighted: method:
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted: highlighted];
// Highlight your labels here
}
Useful link: UIControl Class Reference

Related

Create a custom UIButton with text and icon

I would like to create a custom UIButton looking like this:
The idea is simple, I want the titleLabel to start 24px from the leading, and stop 12px or more to the icon image (which is the square view at the right of the button). It can contain up to 2 lines.
The thing is that I have no idea where I should put this code in a class inheriting from UIButton.
What should go into draw(_ rect: CGRect)?
Also, should I use the titleLabel and titleEdgeInsets UIButton properties and the imageView/imageEdgeInsets as well, or instead use custom properties for this?
You just want these buttons to work, be tapped, and react somehow?
These buttons are custom buttons, and you may a create a Cocoa Touch Class file for them; name it and let it inherit from and be a subclass of UIButton.
Then connect that file to your buttons in the storyboard.
But first, you need to set/attach the newly created file to your buttons. Click on the button, In the identity inspector of your storyboard, with your button selected go to the Class field and type the name of your new file and don't forget to tap 'return' to save your changes.
Now, your buttons are attached to that custom UIButton class.
From now, you can attach all your icon images and many more as outlets to your custom UIButton Class you just created.
Most importantly, now these buttons can be used on their own; they can be outlets or trigger action methods. And to make it look just as you except, you just have to initialize your buttons, i.e. provide an icon image, text, etc.
Is what I wrote clear?
If not, I am glad to help you.

Create custom UISearchBar Swift

How would I create a custom search bar that looks like this:
I could also use a textfield if that would be easier. How would I create a text field that looks like this using Swift?
Create your customTextField class and inherit it from UITextField.
Make its edges round setting cornerRadius for its layer and color it gray the same way.
Add a UIImageView with on top of the search bar.
Accept touches on the view. On touchesBegan, make the textField becomeFirstResponder and make the image hidden to true.
If you want to make a custom textfield or search bar you are going to have to do it programatically. You can add the search bar or textfield to the view but in order to get a customized look, like the one in the picture below. Also, make sure that you have all the code in the ViewDidLoad() and create an outlet for the item on the view. It isn't hard to do and I'm sure you will get the desired outcome as long as you are willing to go through some trial and error!
Don't worry doing all the customization is a bunch of fun, and give a great feeling after you have solved it!
I hope this helps

iOS - change animation when user taps screen

Lets say that i have an animation - an image is going from left side of the screen to the right. I would like to make it a little bit interactive - when user taps on a screen i want to change direction of image movement. Whats the best approach to implement it?
What I do in some cases is take the main view of the View Controller, in Storyboard, and change the class type of that UIView to UIControl.
In the code that is accessed as MyViewController.view, which you can write:
var viewAsControl = myViewController.view as UIControl
In Swift or some equivalent of that.
The UIControl subclass of UIView is the hierarchical layer (class) that adds the action/target facilities to a view. For example, UIButton is a UIControl, because it generates events (actions), and it is also a UIView so it can be added as a subview.
Then from the Connections Inspector, accessed via the far right Icon of the far right panel (that is, the panel to the right of the storyboard editor window), I'd select the Touch Up Inside event type or some other event and drag it to an #IBAction tagged function I'd add to the View Controller's source code, to receive the tap event. From that tap notification, you can cancel the current animation and add a new one, etc...
Alternatively, you can create an IBOutlet for the view if you've turned it into a UIControl in IB, and use the addTarget() method to assign an action handler for a specific event, e.g. to make it call a function in your code.
Either way the effect will be that any time the view is tapped, it will generate the event for you to respond to

iOS add view to Button in IB

I am trying to add a view on a UIButton inside IB. The only problem it doesn't allow me to put in inside the button only on top?
Is this not possible through IB or am I doing it wrong?
It's not possible in Interface Builder. You have to add it in code.
You should not do this:
Do Not Customize Controls by Embedding Subviews
Although it is technically possible to add subviews to the standard system controls—objects that inherit from UIControl—you should never customize them in this way. Controls that support customizations do so through explicit and well-documented interfaces in the control class itself. For example, the UIButton class contains methods for setting the title and background images for the button. Using the defined customization points means that your code will always work correctly. Circumventing these methods, by embedding a custom image view or label inside the button, might cause your application to behave incorrectly now or at some point in the future if the button’s implementation changes.
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40009503-CH2-SW26
If you need to add a UIView on your UIButton you can achieve it in 2 different ways
The easy way is to follow Cyrille answer: you can do it programmatically because IB doesn't allow you to modify a UIBUtton adding a view on it
The hard way is to create your custom button (let me call it "MYCustomButton"), that extends a UIButton, and use it in your application. With this way when you need to modify the buttons in your interface, you can achieve it modifying the XIB of the "MYCustomButton".

Using a UIView as a button

I'm trying to use a UIView I've created in Storyboard as a button. I assumed it would be possible to use a UIButton, setting the type to custom. However I was unable to add subviews to a custom UIButton in Storyboard.
As such I've just spent the last hour reinventing the wheel by making my own custom gesture recoginizers to reimplement button functionality.
Surely this isn't the best way of doing it though, so my question - to more experienced iOS developers than myself - is what is the best way to make a custom button?
To be clear it needs to:
Use the UIView I've created as it's hittable area.
Be able to show a
different state depending on whether is currently highlighted or not
(i.e. touch down).
Perform some action when actually tapped.
Thank you for your help.
You can use a UIButton, set the type to custom, and then programmatically add your subviews...
Change your UIView into a UIControl in the storyboard. Then use the method [controlViewName addTarget:self action:#selector(*click handler method*) forControlEvents:UIControlEventTouchDown];. click handler method is a placeholder for the method name of your handler. Use this method except change out the UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to call a method when the user finishes their click and drags their finger out of the view respectively. I used this for something I'm working on now and it works great.
In Touch down you will want to: highlight all subviews
In Touch up inside you will want to: unhighlight all subviews and perform segue or do whatever the button is supposed to do
In Touch Drag Exit you will want to: unhighlight all subviews
See second answer by LiCheng in this similiar SO post.
Subclass UIControl instead. You can add subviews to it and it can respond to actions
Why are you implementing your own GestureRecognizer? I recommend using the UIView so you can add subviews in the interface builder and adding a UITapGestureRecognizer. You can even do this graphically since you don't care about IOS4 support.

Resources