Adding UIButton to custom cell with auto layout - ios

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.

Related

"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.

Swift UIView disappearing from Stack View

I have a label, an Image View, and a button in a vertical stack view. The alignment is set to fill, and the distribution to equal spacing with an offset of 10. On my storyboard, the label appears on top, the image in center, and the button at bottom.
However, when run on simulator or device, the UIView is stripped from the view entirely, so the label appears at the top of the screen, and the button directly below the label.
My only constraints are set on the stack views to the superview
Edit: Forgot to mention the UIImageView is inside of a UIView
I had a similar problem. My UIView in my UIStackView had only a Width constraint and would disappear at runtime. I found I could make it show up again with either of these options:
Add a Height constraint
Add a Top and Bottom constraint
I was having the same issue with a tableview in a stackview. Added the Height constraint to the view and it worked now everything shows fine
Setting a Y constraint on the UIView border caused it to appear when hitting the play button, but it grew outside of the screen bounds.
I had to set size constraints on the UIView in order for it to display properly.

iOS UITableView cell fit to screen width

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.

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.

Applying autoLayout constraints to multiple objects

I am playing with autoLayout constraints in iOS
My use case is this way,
I will better explain with images
I am using a custom cell for UITableViewCell, through autolayout I have set constraints for text and divider line below and they obey autolayout constraints. Now you can see the yellow vertical strip on the left.
I have not set any constraints for it, so when the cell height is normal, it covers the whole cell, but when cell height increases, it doesnt increase and fill the whole cell. I am not able to make its height flexible w.r.t to cell.
I want to expand the yellow strip to cover the whole cell.
So how I can achieve this.
You can make height of yellow strip flexible by setting 2 constraints.
Set both top space and bottom space to container (cell's content view) to 0.
Ensure there is no constant height constraint for it.
You need to set constraints to the yellow view. By default it would apply its own constraints at runtime.
To the yellow view add TOP , BOTTOM , LEADING , and TRAILING constraint. DO NOT ADD A HEIGHT constraint.
Make sure the TOP and BOTTOM constraints of the yellow view are pinned to the cell's content view.
Let me know if it works.
Cheers

Resources