XCode 8: Views not adjusting when UIElement is hidden - ios

I am trying to conditionally make a UILabel at the bottom of my view controller hidden and the view immediately above that adjust to fill the space.
The UILabel at the bottom of the view controller has a height of 120 and a constraint to the bottom of the view controller of 0. The view above it has a constraint to the bottom of the view controller of 120.
When I make the UILabel hidden and change the value of the view above it to the the bottom of the view controller to 0 (named viewBottomConstraint), the UILabel disappears but the view above it remains where it was.

Instead of changing the label to hidden, just change the height constraint!
You see setting the property to hidden has no effect on the nearby views but changing the height to 0 and calling layoutIfNeeded, updates all the constraints related to that particular UILabel and lays out all the views.
You need to set the height constraint to 0 of the UILabel. Do this by dragging an outlet of the height constraint from your storyboard to your UIViewController and then just:
heightConstraint.constant = 0.0
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})

Related

scrollView not scrolling all my contents

I'm using a scrollView and inside it used a view.
I set sizes like this:
I think the height of my view always is 959.
but when I change the height from 959 to 2000, I see a orange line and when I use Editor->resolve Auto..->update frames my custom hight returns to 959.
even when I set height from pin I see red lines:
I want all my content scrolling. The content's height is 2000.
You check this following way:
Scrollview Constraints:
Set top to your super view
Set bottom to your super view
Set leading to your super view
Set trailling to your super view
Take One UIView into scrollview and apply following constraints(Equal to same Scrollview width and Height)
Set top to scrollview
Set bottom to scrollview
Set leading to scrollview
Set trailling to scrollview
Dont forget to set Equal width to scrollview
Then do add your all UIElements whatever you want.
Consider like below:
lable 1----> set necessory constraint to UIView
lable 2----> set necessory constraint to UIView
lable 3----> set necessory constraint to UIView
lable 4----> set necessory constraint to UIView
lable 5----> set necessory constraint to UIView and you give Bottom constraint to UIView
Then scroll will automatically work..Dont adjust value like that.If you want more large screen Change View controller Simulated size to Freeform and there you adjust your prefer height and place scrollview based on that.

In auto layout, How do I "hide" a UIView?

In my UITableViewCell, I have two UIViews stacked on top of each other. Let's call them Top and Bottom.
The Top view has leading, trailing, and top constraints to superview. It has a height constraint of 20.
The Bottom view has leading, trailing, and bottom constraints to superview. It has a height constraint of 20.
Top and Bottom have a vertical constraint.
What is the easiest way to programatically "hide" the Bottom View (and have the Top View touch the bottom of the superview)? I prefer not to create any more constraints, since I did design this in storyboard, and I prefer not to activate/disable constraints.
If you don’t need to target iOS 8 and below, the easiest way is to embed the two views in a UIStackView. You can then simply hide a view by setting its hidden property and the stack view will automatically update the layout:
The stack view automatically updates its layout whenever views are added, removed or inserted into the arrangedSubviews array, or whenever one of the arranged subviews’s hidden property changes.
Since your parent view is a table view cell, you may have to tell the table view to recalculate the cell heights (unless you’re using autosizing cells, then this may work automatically, I’m not sure). You can force a recalculation by sending the table view an empty beginUpdates/endUpdates pair:
tableView.beginUpdates()
tableView.endUpdates()
The right way:
The Top view has leading, trailing, and top constraints to superview. It has a height constraint of 20.
The Bottom view has leading, trailing, bottom constraints to superview and top constraint to Top view.
Than just make a property for height Constratint inside your cell:
#property (nonatomic, weak) IBOutlet NSLayoutConstraint *heightConstraint;
Than when you need to change the size, call this code:
self.heightConstraint.constant = 40;
[self.view layoutIfNeeded];
or with animation:
self.heightConstraint.constant = 40;
[UIView animateWithDuration:0.3 animations:^{
[self.contentView layoutIfNeeded];
}];
You can increase the height constraint of the top view to 40 and reduce the height constraint of the bottom view to 0. Personally I prefer to have the bottom view height constraint to 20 and add a constraint to the topView bottom equal to bottomView top. And if i want to hide the bottomView I just change the height constraint of the bottomView to 0.
Hope it helps. If you need I can post some pictures in Xcode.

Stretch UIView in a ContentView of a ScrollView

I have the following problem:
I have a Scrollview with Autolayout top,left,bottom,right is set on 0, in this ScrollView is a ContentView also set everything on 0. The ContentView contains a ImageView a Segmented Control and a UIView which is connected to 2 different UIViewController.
The UIView is set top 0 to the Segmented Control and to bottom 0 to the contentView.
My Problem is that the UIView in the ContentView is not streets to the bottom of the ContentView. How can i solved this Problem?
Picture 1:
Storyboard UIViewController with ScrollView
Picture 2:
View on an iPhone 6 Plus
Here there are the size inspector constraints pictures:
ScrollView
MainView of Controller
ContentView in ScrollView
UIView in ContentView
Seems like the problem is in scroll view bottom content inset. By default scroll view have automaticallyAdjustsScrollViewInsets property enabled, and if you insert scroll view as nearest view to tab bar (as I can see at screenshot), bottom inset will set as nonzero (this used for show content under transparent tab bar while scrolling). But for use this behavior properly, you should connect scroll view bottom to view (root view) bottom, not to bottomLayoutGuide. Second option - disable automaticallyAdjustsScrollViewInsets property and handle contentInset and scrollIndicatorInsets properties of scroll view manually (set them to UIEdgeInsetsZero if you don't want change your constraints).

UIIView Inside UITableView Change Frame height to 0 or 60

I have a UIView in top of UITableView (drag and drop in storyboard . I tried to hide and show that view. but table view stands in place . how can i change frame of uivew so when it is invisible tableview goes all the way to top?
PS. I will provide more info for those who are willing to help. Thanks in Advance
http://i.stack.imgur.com/cM3tA.png
![the view with hamburger button and textfield)
With autolayout:
Add height constraint to your UIView with button and textField
Create Referencing Outlet for this height constraint (lets count it will be "searchViewHeightConstraint")
Add Vertical Spacing Constraint between the UIView and the UITableView
In code - when you need to hide UIView - just set its height constraint to 0
self.searchViewHeightConstraint.constant = 0.f;
Also you need to store somewhere the initial height of the UIView (when it is visible) and set its Height Constraint to this value when you need to show UIView.
Without autolayout:
Save the value of UIView frame height
Set the height of UIView to 0
Decrease the y-coordinate of UITableView frame with saved height of UIView
CGRect tableViewFrame = tableView.frame;
tableViewFrame.origin.y -= viewInitialHeight;
tableView.frame = tableViewFrame;

UIView within UIScrollView keeps setting its width and height to 0

My directory structure is this:
UIView
UIScrollView
UIView
Label
Label
I want to be able to center the uiView (the second one) to adjust its width for iphone 6. I set the uiscrollview width and height to constraint with 0 on all sides. Then when i set UIView constraints to all 0, which makes the uiview disappear. I don't know why it's disappearing?
If you are using autolayout with scrollView, you have some options.
First, you can set width and height constraint to contentView, so scrollView gets it as a contentSize. Second (in addition to first), you can fill the scrollView of views with size constraints. Third, you can set contentSize manually. Forth, you can make outlets for contentView size constraint and set them in viewWillLayoutSubviews manually.
So, your view dissappear because contentSize of scrollView is zero.

Resources