iOS UITableView cell fit to screen width - ios

I'm new to iOS and trying TableView for the first time.
I added a prototype cell into the TableView. It has label a inside it which is on the right side. When i run it on lower display like 4S, the label is not visible.
I tried setting Horizontal Space - Content View to label (on right side), but has no effect.
I been playing with the auto layout for sometime, but can't figure it out, can someone be kind enough to help me?
Edit:

Before answering straight, I would say stick to autolayouts and not look for frames, springs and structs.
Now pin UILabel. Select UILabel Go to Editor>pin leading and top space to superview. Also pin the width and height (fix them if you want to). Your problem is solved.
Go through this book for autolayouts and keep playing.- iOS Auto Layout Demystified, 2nd Edition
Updated after question edit :
For Label1 set :
Pin Leading, Top and Bottom Space to superview.
Pin the height(not the width)
For Label1 set :
Pin Trailing, Top and Bottom Space to superview.
Pin the height(not the width) again.
Now select both the labels and go and pin horizontal spacing between them. And if meanwhile you update frame... do update constraint also.

give vertical constraints to your label, if there is not that much need of autolayout you can disable autolayout.

The constraint is not enough to determine the vertical position of the label.
Two optional ways:
Add the Vertical Center in container constraint
Add a height constraint of the label and a top spacing to containe constraint (the contentView of the UITableViewCell your case)
With each of the above and your already exist constraint, the position of the UILabel can be determined exactly.

You can add leading, trailing, top, and height constraints on your label.

Related

Constraint for StackView in Xcode so the buttons stay always to the center of the view vertically?

So i created a simple UIView. I added two UIButtons. I have added them in a StackView. I have put the Alignment of the stackview to fill in xcode attributes inspector and the distribution to fill equally and the spacing to 50. Then i added a height constraint of 300 to the stackview and i added a custom font as well to the buttons as well as three constraints. One for the stackview bottom as you see at the screenshot, one for the trailing and one for the leading constraint. I haven't put a top constraint yet. When i change to different devices through xcode the buttons are not centered. I added the top constraint but i guess because of the height constraint at the stackview there is an issue and all constraint become red when i change to another device than the initial i created the stackview. If i move for example to Iphone SE the buttons move to the top side i guess because of the bottom constraint... So is there a way to center the stackview with the buttons as i change to different devices? Any help appreciated.
For a stack view to be centered you needs leading, trailing and vertical centering constraints, stack view will have automatic height based on buttons heights and vertical spacing, no need for bottom constraint

"Scrollable content size is ambiguous to "ScrollView"

I am a little stuck with the ScrollView in Swift and Xcode 11.
My goal is a simple ScrollView with Labels and Textfields. Each label describes what the textfields are used for. For example there is a Label "Name", so you have to enter your name in the textfield under the label. For this I need a ScrollView because the App I am coding requires a few more information. When I am trying to put this in a ScrollView I am constantly getting the following error:
"Scrollable content size is ambiguous to ScrollView".
In order to find help I searched the internet and for example found this question: UIScrollView Scrollable Content Size Ambiguity.
Unfortunatly it didn't solve my problem. So I decided to create a small test project with the following:
- I created a project as a Single View Application
- I added a ScrollView to the ViewController
- I clicked on the constrains-Button at the canvas
- I selected 0,0,0,0 for leading/top/trailing/bottom.
- I clicked on "add 4 Constrains
- Now the warning appeared. Also the ScrollView was not resized to the ViewController... I don't know why.
- I added a UIView inside the ScrollView and set the same constrains, this time according to the ScrollView.
- The UIView (Content view) got an extra constrain: Equal width with the root view
- Now I added two Buttons, one with the text "Hello" and the other one with "world" on it.
- The "hello"-Button received the following constrains: 16 to leading, trailing and top, as well as a fixed height of 30.
- The "world"-Button got the following constrains: 16 to bottom, leading, trailing and over a 1000 so you can scroll through the ScrollView.
After all these steps the error is still there and if I run the app the "hello"-Button is at the top of the screen and the "world"-Button is at the bottom and I can't scroll.
Can maybe anyone help me fixing this issue. I am looking forward to every answer! Thank you for your help in advance!
Here is the trick that worked for me:
Add a ScrollView to your wished ViewController.
Select it in the Outline and open its size inspector.
Uncheck there the option "Content Layout Guides".
Now set leading/top/trailing and bottom constrains to 0 of the ScrollView.
Add in a UIView and constrain its leading/top/trailing and bottom also to 0.
Add an equal width constrain to the UIView. (The width needs to be equal the width of the view from the ViewController, with this way you are disabeling horizontal scrolling).
The warning will disappear if every element inside the UIView is chained vertically. This means, that the top element has a constrain to the top of the view and to the element under it and so on. The last element needs a constrain to the bottom of the view.
If you followed this steps you should be fine with ScrollViews. This way you also can add as many content as you want to the bottom and the ScrollView will extend dynamically.
I hope I explained it well enough.
It sounds like you're on the right track, and really close.
The first part is absolutely right - you add your ScrollView and pin it to zero for the top, trailing, bottom, and leading constraints. That makes it take up your whole screen.
Then you drop a plain old UIView into the ScrollView, and pin its top, trailing, bottom, and leading constraints to the ScrollView (all as zero again). Then you set the UIView to have an equal width to the ScrollView. The last thing is to set the UIView's height as equal to the ScrollView's height, but you change one thing: you set the priority of this constraint to be low (250). That's basically what allows the UIView to exceed the size of the ScrollView, so you can then scroll.
All you do then is add your buttons, etc inside the UIView, so you place them in relation to it rather than the ScrollView, and pin or align them as you wish.
Add a total of 9 constraints
1 - 4: ScrollView to Superview (leading, top, trailing & bottom)
5 - 8: Content view to Content Layout guide (leading, top, trailing & bottom)
9: Content view Width equals width to Frame Layout Guide.
But when you do this (right-click drag and drop) 7 & 8 constrains (trailing & bottom constrains to Content Layout guide) will have constant values. (check image below).
Just make them zero. Now it worked.

Multiline UILabel with UIScrollview using autolayout

I read many post of contentview in scrollview,but the problem is slightly different.check out the image
and i have following hierarchy
Here the label is multiline label and its fill up with too many content size.
so, the problem is
1. according to the lines of the label, height of label changes and
2. accoring to height of label, contentview height changes.
so, that it scrolls. and i want this things to be done with autolayout.
And one thing i want to mention is , i'm using ios 8.1 and xcode 6.1
The thing that you can use for your UILabel is -> Select your Label and go to Attributes Inspector and find Autoshrink set it as Minimum font size then you may describe your minimum font size.
For addition to your view, you may want to use constraints between your label and content view. So I recommend to use this documentation Auto Layout Guide. And if the documentation is too long to read you may read this answer.
You should add constraint to content-view as leading, trailing, top, bottom & equal width & equal height with scrollView. For imageView add constraint as leading, trailing, top & height. For the Label add constraint as leading, trailing, top & bottom, also check that you are setting no of lines to be zero. & For read more button add constraints as height, width, centre horizontally in container & bottom. Don't forget to add bottom constraint to button otherwise your scroll view does not understand its content size & you will get warning as ambiguous content size to scroll view.

Adding UIButton to custom cell with auto layout

I am trying to add a button through IB in a custom cell at top right corner of the cell. I am using auto layout to position it correctly but in simulator I don't know why the button wont show up.
But if i create it programmatically then it shows up perfectly.
So my question is how can achieve it using IB and where i am doing it wrong.
I have added four constraints on my button leading and trailing space to superview and fix width and height.
Remove leading space constrain and add fix to top space constrain. I mean there will be total 4 constrains Fixed width, height and fixed trailing, top space to super view and it will work. If it is not working means you have not set your tableView constrains. For tableView fix it from top,bottom,left and right space from super view.

Keep UILabel centered using Auto Layout

I'm trying to keep my UILabel centered horizontally. I have tried in IB setting leading and trailing, but that simply stretches out the entire label, and I need it to stay the same size. Everything is set up using Editor and the Pin function in IB and not coding. Suggestions?
If you want to keep all the time buttons together and aligned to the center. Put them all in their own view, arrange how you wish. Then fix the length and width of the view and center it in the container like Aaron suggested. I do believe you will need to make one more constraint. Perhaps pin the view to the top. Hope this helps.
Seems too obvious, but did you try nsconstraint of width = XXX. Label should say same width and the leading trailing should keep it put.
For keeping a UILabel horizontally centre, add the following constraints:-
Width constraint to the label.
Height constraint to the label.
Top/Bottom Constraint for placing the label vertically.
Horizontal centre in container.

Resources