ObjC - How to show/hide view that has proportional height constraint? - ios

I have a view that has a proportional height constraint of its superview and the multiplier is 0.09. And there is a UITableView below that view.
When I press a button I want to hide the top view completely and when I tap that button again I want to display it again.
Since it has a proportional height constraint and has a multiplier, I cannot directly change its constant. I need to create a new constraint and assign that constraint to that view. I have tried changing multiplier and assigning it, it worked and hidden the view but when I wanted to display it again and set the multiplier to 0.09 it didn't work.
Do you have any solutions?
Thanks.

first off, you shouldn't code a view like that above the UITableView that will be shrunk and then force the UITableView to resize. Doing it this way is prone to brokenness. The easist way to achieve this is make the view at the top a header of the UITableView. And when the button is pressed, you "realoaddata" on the UITableView which will then call the header view height delegate method where you then change the height based on a boolean value or something. Don't do what you're doing. also, use a collection view instead. good luck

Related

How to shift UIView's and put a new UIView programmatically with Swift?

I've a layout with my data, but depending on data I need change the location of views. I wants put a UIView (UILabel, UITextField...) at top of screen and shift the layout to down, but I don't know how to do this, I saw some docs and tutorials but not helps me. I made my screen with storyboard.
This is original screen:
This is that I want:
My storyboard:
If you just want to add the view at the top of your stack view, use stackView.insertArrangedSubview(newView, at:0)
I would suggest setting up your screen with your "put other view here" in place, and a fixed height constraint on that view. Make the views below have a fixed space to the bottom of the "other view here view."
Then control-drag from the "put other view here"'s height constraint into your view controller. This will make the constraint an outlet.
Now edit the height constraint and set it's constant to 0. This will collapse that view to 0 height, causing it to disappear, and the other views to shift up.
Then, in code, when you want the "other view here" view to show up, use the outlet to set the constant on that view back to it's previous non-zero value, and then call layoutIfNeeded() on the parent view to update the view layouts based on changed constraints.
You could also write code that would insert your "other view here" view into the view hierarchy, but that would involve removing constraints and adding constraints in code, which is a bit of a pain.
By adjusting the height constraint on the view you can show/hide it back and forth with very little effort.

Autolayout. Set two views vertically with dynamic height

I have a view1 which can have dynamic height depending upon content with in it. Just below that view, I have to show the table view. Top space constraint of tableview is view1 and bottom constraint of tableview is view 3 that has fixed height and stick to the bottom.
The problem is that I can neither set height constraint of view1 nor tableview as top view can be dynamic and tableview have to take remaining height that varies in different device and I am getting error:
"Need constraint for Y position or height" for both view 1 and tableview. Although I have set costraint for y position for all views.
How should I solve this.
Add a constraint so that the top of your tableview is adjacent to your view 1.
Create a constraint for view 1's height. Doesn't matter what value you pick, just pick something.
Create an outlet for your constraint in step 2.
You should now be able to programmatically update the constraint in step 3 to have whatever value you really want for the height, and the rest should happen like magic.
4a. You might need to call layoutIfNeeded to make the view redraw and relayout things.

UITableViewCell dynamic height + hidden elements take up space in Swift?

This is the scenario: I have a Table in which there are Cells with dynamic height. In each cell I have a label and a switch. When I run the App only these two can be seen but then I set the switch to true then a (hidden?) textview appears under these two elements making the height of the cell bigger.
How can I do this? Because when I try hidden elements take up place as well.
This is what I want:
Reality:
How can I do this?
I tried using the new Stack View which can handle it fine, BUT NOT in a table cell....
How can I solve this?
I solved it mixing this answer: Dynamic Height Issue for UITableView Cells (Swift) for the dynamic height and then for the views that show or not I added a height constraint to the view and then on cellForRowAtIndexPath just set the constant of the constraint to 0 if you need to hide it or to your height if you show it.
Hope it helps!
I think it could be solved using constraints:
- set the height constraint for your text view,
- create the outlet for that constraint in your view controller,
- change the constant property of the constraint in your view controller when the switch is used.
If you hide your text view, your text view is still there, text length is as long as it is not hidden.
So instead of hide/show the text of the text view, you'd better to update the text of your text view from "" -> "what ever you want to show."
In this case, the auto layout can handle your query fair easily. You only need the set
self.tableView.estimatedRowHeight = 65 //whatever number
self.tableView.rowHeight = UITableViewAutomaticDimension
and in the cell xib/storyboard, add constraints on thetop andbottom of your text view, don't also forget to make your text view not scrollable as well.
Actually, I recommend you to simply useUILabel in stead ofUITextField, you just need to set thelineNumber of theLabel to be 0, and you can neglect the scroll and size setting of the text view.

iOS Swift - bottom menu like skype

I want to create bottom menu like in Skype application.
It should be at bottom with some icons and it could slide up to show more items.
What I've created is UIView with height constraint. When there is swipe gesture or dots are tapped then I change constraint of Menu view:
topMenuViewHeightConstraint.constant = 200;
UIView.animateWithDuration(0.5) {
self.view.layoutIfNeeded()
}
It's working and looks good. But I am not sure if this is correct solution. If there isn't something better. Is that animation done correct way? Would it work good if there be more controls on screen? What If I want faster animation from start and then slow ending? Thanks
This is indeed the recommended way to animate constraint changes. I would suggest a slight change in how you approach the constraints.
Instead of manually assigning the view a height, you should let auto layout generate the height for you, and use a constraint pinned to the superview.bottom to perform the animation.
When user taps your UIBarButtonItem create the view and add it to the superview with a top constraint of 0 to the bottom of the superview. Now the view is positioned "below" the screen and not yet visible to the user.
Call layoutIfNeeded() on the view to trigger auto layout
Grab the height with CGRectGetHeight() and use it to set the top constraint's constant to the negative of this value (e.g. -400).
Call layoutIfNeeded() again inside your animation block and the view will slide up from the bottom of the screen.
For gestures you can use the same approach and simple use the UIGestureRecognizer method translationInView() to adjust the top constraint's constant accordingly.
An alternative method that doesn't require referencing the height in code could be to remove the top constraint and add a bottom constraint to superview.bottom.
Also -- see my other answer to a similar question:
https://stackoverflow.com/a/28484328/1451954

Autolayout reseting views after changing frame in code

I am trying to change some frames inside the code but I want autolayout to be respected.
I have this hierarchy
When I hit the search button on top right of navigation bar, I want the green view to go up 50px. It is working great but the tableView is not following the autolayout since its top is binded on the green view.
So when I hit the button I have a blank space between the navigation bar and the table view.
Any ideas how can I sort this out using autolayout?
When using auto layout, frame is dynamic based on constraints. Each time the view layout, it will recalculate by constraints and update view's frame. So instead of changing the frame of green view, you should change corresponding constraints.
Make a constraint reference in code call topSpace which is the constraint between green view and top layout guide and set its constant to -50 when user tap the button.

Resources