How to hide view graphically while maintaining presence in stack view? - ios

I currently have a stack view that is horizontal, fill, fill equally. Inside, there are 3 generic views with different coloured backgrounds.
My current problem is that, when I set one of the views inside to hidden=true, the other two views immediately expands to fill the void. I don't want this. All I want is for the space to remain blank and the target view to be "invisible" both graphically and to UI input events.

Instead of setting isHidden=true you could set view.alpha = 0 to make it invisible and also view.isUserInteractionEnabled=false to disable UI input events.
*As #robmayoff pointed out below the isUserInteractionEnabled=false is not actually required.

As I can see in your screenshot, you are using the constraints. So, when the element hides, collapses and the other expand.
Use 'opaque' flag to make it invisible.

Related

Why are the View elements disabling interactive elements?

I am just playing a bit with the constraints and auto layout within iOs. I made up a design which caused the interactive elements (except for the buttons) to be disabled. I cannot toggle them.
First they were working, but then I applied a few Views and constraints... I colored the Views underneath the disabled elements. So programmatically I did not make a change.
What did I do wrong?
EDIT
Here the constraints of the back and save button:
Mr #DonMag pointed me at the right thing:
The little Red arrow-circle-icon means you have a conflict with your constraints. That isn't necessarily the reason you cannot interact with objects, but it certainly could be related.
The little Red arrow-circle-icon pointed me at a missing constraint for the height of the underlying elements view. Once I set the height of the View, the elements where enabled again.

How to bring all desired UI Interface objects and views to the front?

I have a background image for the view controller and two labels. The two labels are not showing up because they are behind the imageview. When I go to editor->arrange the send to front option is not available for the labels. And when I check on the layout debugger I can see the labels are behind the imageview. Is there another way to bring to the labels in front?
Assuming you are using IB, you can move things to the front/back of one another by rearranging them in the left panel. The first item in the list will lie behind every other item. So in this case your list of subviews should go:
1. Image
2. Label
3. Label
This will result in the labels lying one top of the image.

Best approach to showing or hiding dynamic content in iOS

I've been doing iOS for a while now, but when it comes to dynamically hiding / showing elements, I'm a bit lost.
Coming from Android, I'm used to being able to simply set views to visibility gone, but this doesn't exist on iOS.
So let's say I have the following scenario:
Basically I want to have a table, but the table should not fill the entire view controller. Instead it should leave place for optionally either a button, a multiline label, or possibly both at the bottom (if visible, these should be fixed, not scroll).
One way to solve this would be to use auto layout and modify constraints, like adding a zero height constraint. But that would make iOS kill one of the other constraints, which would make it hard to change it again. For the label, I wouldn't always want to have a height constraint, because it could be multiline, and should take the size it needs.
Maybe it's easier to skip autolayout here and modify frames instead, I don't know.
My question is: What approach would be best here?
Is there some other way of doing this I haven't thought of, or do I have to try to do what I described above?
I'm not primarily looking for code (code can be ok), but I'm more interested in a description of how it can be done.
I'd like to support iOS 7.
This problem had a variety of solutions, and opinion based, but I'm facing such questions a lot, when I don't know what to choose and what would be the "right thing".
So, I my opinion, the best solution here is using autolayout, you need to set height of label manually, but you have a few methods for this, at least you can play with it and if you don't succeed ask question about it. Using frames, you'll face same problem of calculating height, right? But with auto layout, you only need to set height, vertical space to 0, when you need to hide message.
You can also use constrains with priority lower 1000, and remove completely constraints from message (button, label) if you don't need it at all anymore.
For example, taking your layout image, you can make UIView with subviews: button, label. Top constraint connect to the UITableView, other constraints to the sides.Label and button will calculate the view's height. The only question here is label height.
So in ios assuming that the background of both these objects is opaque only the front most view in the Heirarchy will be visible and interactable, An easy solution would be to change the different frames of these two things you need and make sure they are in the back of your view heirachy, and when you need them to appear use view.bringSubviewToFront(mySubview) and view.pushSubviewToBack(mySubview) to make it disappear again. View obviously would be referring to main view of your view controller.

moving image behind label

I have an image on my view, which I add programmatically & and not through storyboards. The image gets animated to move up the screen. At one point it crosses paths with several buttons and labels, which contain important text that gets blocked by the image passing over them. I want the image to move under the labels and buttons so that the text remains visible. Based on this answer IPhone Interface Builder: Z-Index, Z-order of a button, image, ui element, etc?, I tried to use editor => arrange => sendToBack/sendToFront, however, I don't think this is having any effect because the image doesn't appear in the tree of elements in the storyboard. As noted, it gets added programmatically at a certain time. Is there a way to do this?
I believe you can use [view insertSubview:imageView atIndex:0] which will place it behind all other subviews. Depending on what other subviews you have, you may need to increase the index as it controls what is placed over what in the view (lower index will go behind higher index)
As in answer to this question: Difference between addSubview and insertSubview in UIView class
If you're using addSubview:, your view will be placed on top. To control it's depth use either insertSubview:atIndex: (at exact place from bottom) or insertSubview:aboveSubview: or insertSubview:belowSubview: (which places new view above/below another known object in view hierarchy).

How can I dynamically add/remove a button in a UIView and reposition the buttons below it and vertically resize the view?

I have a UIView, defined in a nib, and I need to be able to show/hide a middle button in that view. When I show the button, I need to reposition the two bottom buttons below it, as well as make the view taller to make room for everything. When I hide the middle button, I need to move the two bottom buttons up and vertically resize the view to make it less tall. I do NOT need to animate any of this since it the changes will never occur while the view is visible to the user.
I'm new to iOS and I'm used to using Autolayout, but I can't use Autolayout in this case to handle this automatically, so my current approach is to hardcode the frame position and dimensions for the two bottom buttons for each of the two different situations. I'm also hardcoding the two different frame sizes for the view itself. In viewDidLoad, I determine if I need to show/hide the middle button and set the frames for the view and bottom buttons appropriately. This works, but if feels hacky. Is there a better way I should be doing this?
Thanks in advance for your wisdom!
You don't need to hardcode your frame sizes in viewDidLoad. The only thing you should take care of is determining that whether you need to show you middle button or not. Within the implementation file where you are allocating your UIButtons, check if the middle button has to be shown, if Yes allocate it, if not then don't. The frames of two buttons and the view should contain a factor which can set/size them accordingly.
You'd basically be managing the Auto-layout programmatically. And if you're not even allowed to that then whatever else you'd end up doing would pretty much be a hack.

Resources