What do these settings do in Interface Builder? - ios

This is for a UIButton specifically. I understand that the alignment options pertain to the label inside the UIButton, but what does the 'content' settings do? They seem like they correspond to the setEnabled:, setSelected:, setHighlighted: methods of the UIButton class, however clicking Highlighted or Selected doesn't seem to change the button's behavior outside of interface builder. Also, what would be the use-case for having a button always show highlighted or selected?

There are several practical uses for these methods. As #MHUMobileInc. pointed out, it can be used for a game like Minesweeper. It can also allow you to use the button as a sort of switch, where the selected button has "ON" and the non-selected version has "OFF". It seems that Apple wants developers to shy away from this, and to use either UISwitches or a custom view. However, Apple does contradict itself in some of its apps.
These settings set the initial state of the UIButton- so if in your viewDidLoad you set it not selected nor highlighted, this Interface Builder setting will not be seen on screen when you run the app. If you want to ensure that these settings are kept, it might be easier to say so in code (viewDidLoad) rather than Interface Builder.
It may be easier not to use these settings too much. They can lead to confusion for you (the developer), other programmers who are working on the project (if any) and the user. Use the built-in UIKit views when it works and create your own subclasses when there is nothing that fits best for the situation.

The upper portion addresses the alignment of the content within the view. For example, if you have a large UIButton with an image that's smaller than the bounds of the button, by default the image will be centered vertically and horizontally inside the button. However, if you want the image to hug the left/right/top/bottom, you can set the alignment using those options.

All of these controls pertain to the UIControl, not UIButton, as indicated by the header in that screenshot.
Alignment refers to contentHorizontalAlignment and contentVerticalAlignment. Many controls return a fixed width or fixed height, or both, from sizeThatFits:. For example a UISwitch object has a fixed size, whereas a UISlider object has a fixed height. If you assign a frame to a control object that does not correspond to these fixed dimensions, these properties determine where in that frame the control interface should be drawn. Instances of UIButton do fill their frame, so the UIButton class interprets these properties differently to apply to the button's content, but the properties themselves are not specific to the UIButton class.
Similarly the selected, highlighted and enabled properties are defined by UIControl not UIButton, not all subclasses of UIControl utilise them.

Related

How to make UITableViewCell read out a UIImageView automatically the way it does for UILabel with voice over

I've tried adding random labels to a UITableViewCell and the accessibility voice over system reads all of them automatically.
I'm also trying to add a UIImageView as an accesibility element and would like it to be read automatically with the cell as well. It's basically a little status icon.
I set the accessibilityLabel on the image.
I also set isAccessibilityElement to YES
Right now it treats the image as a separate object. I can select the image and or swipe left and right to read it separately instead of it being read with the cell.
Returning it in a custom accessibilityLabel getter for the cell should work too, but I'd like to use the automatic solution as much as possible. Is there some property to set on the UIImageView to have it act like a UILabel and be automatically read as well?
I think you have two options, one is a bit unclean, being create a label that is hidden with the same text and read through that.
I think the cleaner option for you is to maybe subclass ImageView and just give it a text or label property and have the accessibility voice just read over that instead.

Cannot move content view in cells in Interface Builder

I can't change a position and size of title and subtitle label in Interface Builder. What should I do?
You can't edit the position of the default UITableViewCell subviews in interface builder. You can change the text alignment, content insets, and other related properties but you can't explicitly change their frames. You can do this in code when you are displaying the cells themselves as some other answers point out, but I recommend against doing this as it can cause some other side effects when Apple makes changes to these components.
If you need to customize the position of these subviews, you should instead just build them manually. You can do this in interface builder by selecting the cell, then selecting the 4th tab on the right (Attributes Inspector), then change the Style to Custom. Once you do this, the default subviews will disappear in interface builder and you can just add your own.
You have chosen the default cell style for your cell. The Title and Subtitle labels for the chosen style you see are some basic UI controls that are meant to be used for typical scenarios of displaying content in a tableView usually. If the framing of the labels are comfortable and okay to you, then it is fine. If not, you need to make your own custom style cell.
Basically you cannot modify the default cell style that Apple provides. Just make the cell style Custom and do whatever you want with the layout.

How To Properly Show/Hide UI Controls & Handle Constraints

I'm building a realtime IOS dashboard and I need to dynamically display UI elements (currently a UISlider or UISwitch) inside a UITableViewCell depending on the data retrieved. The tableview cell (and associated XIB) has a label on the left side and either a UISwitch or UISlider on the right.
I'm able to show/hide the UI elements via the isHidden property of each component but the autolayout constraint always places the UI elements next to each other even though in the interface builder I placed the UI components on top of each other (because I need to show either the slider or switch not both). BTW - I can't use multiple UITableViewCells due to some RXSwift issues.
The 'hidden' property will not change auto layout constraints.
The control will be hidden but will still take up the space it's meant to take.
Having said that, the UIStackView component tracks it's children's 'hidden' property and changes the constraints whenever the property changes.
You could put your slider and switch inside a UIStackView and set either one to visible or not.
I wouldn't do it that way though.
I'd construct two kind of cells, one with a UISwitch, and one with a UISlider and use a different cell based on the content.
In Swift, The 'hidden' property is not a dynamic var. you can't observe it, but you can try to observe property setter.
details:http://simcai.com/2017/03/29/2017-03-29/
let view = UIView()
view.rx.methodInvoked(#selector(setter: showAllView.isHidden)).subscribe { (isHidden) in
print("\(isHidden)")
}
view.isHidden = true

Custom UISlider with pips ios

Wondering if the above can be created using UISlider? If not, what other ways can this be accomplished?
You can set components of a UISlider, such as the currentThumbImage ( see: "Appearance of Sliders"1).
However, it is almost certainly easier to just re-implement a slider for this much customization. Simply use background UIImageView with the scale image, and then add a separate UIView (or UIImageView) for the arrow. Finally, attach a UIPanGestureRecognizer to the arrow view to allow a user translate the view vertically.
You can change a lot in the appearance of a UISlider like setting the thumb to a red arrow. You can also replace the background image with the inches ruler and with different rulers for the different device types and display sizes.
The one thing that I don't see is that you turn the slider to work vertically. I know them only working left to right.
If I'm right, your only chance is to have a ruler as background image and a view that contains the arrow and a label with the actual value. That whole view can be pawned and tapped using Gesture Listener.

iOS: Combine two UIButtons to one UIButton

On the storyboard I have two UIButtons. In my UIViewController subclass I have one IBAction method for this buttons. One of this buttons has an image and hasn't got a title, another has the title but hasn't got the image. So, it's simply image that behaves as a button and ordinary button.
Both of this buttons call a method. The problem is that if I push one button, another doesn't highlight. Another problem that this buttons have padding and for me will be better if this padding will be touchable (I mean space between image button and title button to be clickable).
I know that I can subclass UIButton and make my buttons as I want, but maybe there is a way to make what I want without subclassing?
Thanks.
1)
Assign both buttons to properties.
When an IBAction fires, you can set one or both buttons to highlighted via those controls' "highlighted" properties, or via setHighlighted:.
2)
As for making space between the buttons touchable, set the alignment for the button graphic to left aligned or right aligned (the latter is what I've done in my example below) and these two separate buttons have edges that are touching.
3) Or...
You can cheat and have simply one button and put some space between your image and your button text, like this:
Yes, that's a whole bunch of spaces (which I've highlighted in green) before the word "Button".
Subclassing is a good thing. If you need a button with both an image and a title, then by all means create a subclass of UIButton that does what you want. You do that once, and then use it anywhere that you want. The alternative is to muck around with stacked duplicate buttons everywhere you want this look.
I found the most suitable variant for me and I think for all, who encountered the same problem. If you have one UIButton for image and another UIButton for text(title label), you can combine this two UIButtons to one Custom type UIButton. Then you can set image and title label text to this custom button. After that, use Insets to layout your image and title label.
For example, to set title label under image you must set Inset Left parameter as negative value and Inset Left parameter as positive value that is greater than image height. With Insets you can set proper image size too.
Using this way you don't need to handle highlight property changing by yourself.

Resources