Autolayout. Set two views vertically with dynamic height - ios

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.

Related

Layout errors when pinning UIView edges to UITableViewCell contentView

Got a problem here which is driving me crazy.
In a storyboard I have a table view in a layout and I'm trying to pin the leading, top, trailing and bottom constraints of a UIView to the prototype cell's content view. Straightforward enough.
Whenever I do this I get these layout errors for the UIView in Xcode:
"Need constraints for: X position, width"
"Need constraints for : Y position, height"
Constraints on that view (Superview is contentView):
In the storyboard I have the following set on the table view:
Row Height: automatic
Estimate (row height): 50
And on the table view cell:
Row Height: 50
I've seen a few SO posts and other walkthroughs that state that pinning the edges is what to do, and as I understand Auto Layout, I've provided the necessary constraints and info.
How can I fix this?
(BTW: I'm running Xcode 11.3.1.)
Update: The cells of this table view will be fixed height. I'm looking for the cell to provide the height for its content.
UIView by itself has no intrinsicSize - so you are telling the cell's contentView to pin to a frame of 0 x 0.
If you are going to add content to the UIView, set the constraints on the content to control the size of the view and the errors will go away.
In the meantime, give the view a height constraint and that will resolve the issue.
Edit
For fixed-height cells... try starting fresh.
Add a table view - give it desired constraints
Add a table view cell prototype
Add a UIView to that cell (give it a background color to make it easy to see)
Constrain the view at Zero on all 4 sides (constrain to margins)
Set the cell Row Height to 50 (un-check Automatic)
Here's how it looks for me:
It turns out I had the table view and cell constraints and settings configured properly; the problem was further up the view hierarchy.
The table view was placed in a vertical stack view, and that stack view's Alignment property was set to center (for the benefit of other views in the stack). This was causing issues for Auto Layout in calculating the width of the table view.
The solution here was to add an Equal Widths constraint from the table view to the stack view. Et voila! The errors on the UIView within the table view cell's content view disappeared.

Can't position UIView to the bottom of TableView

I tried to position the UIView to the bottom of the tableView but it always position itself to the bottom of the Navigation Bar
Promocode View is the UIView
Location TableView is the tableView
After I added the constraints to the bottom of the TableView
What should I do to position it nicely under the tableView?
You are in the correct path here. Adding constraints to the promo code view was the right thing to do.
However, once you started adding constraints to a view, IB tries to figure out the frame of the view basing solely on the constraints. If it can't, those red lines appear. You've only added this constraint:
right? This constraint says that the promo code view will always be 4 pixels under whatever is above it i.e. the table view.
The constraint you added defines the y position of the promo code view. To figure out its frame, you still need three more things:
x position
width
height
For the width and height, you probably want them to be fixed at the current value, so add these two constraints:
For the x position, I think you intend it to be centered? Then move it to the centre and add this constraint:
You also need to add constraints for the table view as well. I suggest a top, leading, trailing and height constraint.
After you're done, it should look like this:

Two uiview not fitting for all screen with same size

I am new to iOS. I am learning my self. No resource are to clear my problem.
I have two view with 270 X 338. that I have to keep in Horizontally in Viewcontroller. But, when I add some constraints it's not fitting for below 5s. I checked in simulator also in preview option.
When i use autoresizing, also it's not fitting for below 5s screen. Please help me with some idea. I need to do only in storyboard.
Thanks
I tried this tutorial Here but it din't help
This is my preview screen
Edited:
Constraints for Back View;
These steps will help you align two views.
Add top, leading, and height for first and second views.
Add trailing constraint from First view to Second view as 8.
Add Equal Width for First view and SuperView and set its multiplier to 0.49* and constant as 0.
Add Equal width for two views again by selecting two views.
Finally update constraints the view will align for all sizes.
0.49 will leave 8 points as trailing constraint.
Constraints for First View
Constraints for Second View
Edit :
For third step you can do by selecting first view and hold Control button and and move mouse to super view.
The selected view in the below image is the Superview
View 1
View 2
you can do it by adding contraints like this also
Use leading, top and relative height and width constraint that will solve your problem... set relative height and width in view with autolayout from below steps
Set equal height and width with superview.
Go to properties of constraint where you find a property multiplier in which set exact multiplier value which you want or which satisfy your constraint.
This will make height and width of view relative to superview that changes according to superview.

IOS scrollview ambiguous scrollable content height in case of autolayout

Hi I am developing small IOS application in which I am using scrollview with auto-layout.Inside scroll I am adding two more views. I am using IB and auto-layout constraints. I am adding two views in side one after another in vertical manner. I have added outer constraints like trailing, leading, top, bottom space. I also added height constraints for both views. Till this everything is working fine.
But my view1 has some dynamic content. For that reason I want to make height constraint greater than equal to instead of equal to.
So how to resolve this problem. Need some help. Thank you.
You should follow the approach below.
First of all, here are some important things about scroll Views which are important for auto layout:
UIScrollView changes its bounds automatically.
UIScrollView needs a content View(in UI) for getting content size for scrolling which works smoothly for auto layout.
UIScrollView's top and bottom constraint should connected to top and bottom layout guide (For most of the cases, not all).
As per your problem:
First Approach: You have UIScrollView, so just insert one UIView inside it and consider it as Content View. After that put your two UIViews inside the UIView (Content View).
So the Hierarchy is: MainView --> UIScollView --> UIView (ContentView) --> firstView & Second View. Now we are going to give constraints to all of them.
For UIScrollView, connect TOP and BOTTOM constraints to TOP & Bottom Layout Guide and LEADING and TRAILING to the Main View.
For UIView (Content View) it is very important to give constraints LEADING, TRAILING, TOP, BOTTOM to the UIScrollView and to give the explicit height (normal height constraint) to your contentView which is appropriate for scrolling (e.g 1200). Also make it horizontally center in container.
Now give constraints to your first view: LEADING, TRAILING, TOP to ContentView and give explicit height (normal height constraint). Don't try to modify it in greater than equal right now - we will do this later. Then, give constraint to second view Leading, Trailing to ContentView, Top to FirstView, Bottom to Content View and Explicit Height (what ever you want). Now, try to modify the Height constraint of first view from equal -> greater than equal - it will definitely work.
Second Approach (Easy and Simple): After giving constraint as per first point, for changing height of first View dynamically you can create an IBOutlet of height constraint of first view to your class and as per your requirement you can change the constraint's constant value (as per you want the height of first view) in any method or button action so it will change in run time. You can also consider it is a fine trick when you want to hide your views so just change their Height constraint's constant to 0 so it will hide and at the time of unhide, again set the constant value of same to desired value so you can also easily play with hide and unhide functionality of view which is little bit difficult in auto layout from other ways.
I would make this constraint as type equals and give it low priority. Then, during adding dynamic content, you may just add another constraint with higher priority.
If you load this UIView from xib just make sure you provide constraint with higher priority.

Autolayout TableView Issue

I'm having a strange issue with my tableview, debugging the view hierarchy i'm getting this
I've tried a lot of constraints with no result, how should I setup my tableview to avoid that kind of misplacement ?
Edit
I've a custom cell with an image view, actually set just this 5 constraints
Top, bottom, leading and trailing values are 0
You just need to set Your UITableView constraint to Leading,Trailing, Top and Bottom. and also for your UIImageView set same constraint. so your imageview will show fit to cell as per table width. no need to set Align Center X constraint.

Resources