Table view cell with label and textField and equal space between each other - swift

i want to create a table view with cells which have a textField.
What drives me crazy is the spacing between label and textField cause i want a equal spacing in all cells.
To better explain take a look at this screenshot.
So im searching for a good way to get the spacing between Second Label and Second textField and set this on the other cells, too.
Is there a way to do it in the storyboard and not by edit the constraint in code?

I would suggest that:
use a horizontal stackview to hold the label and textField with some spacing in between, like 10.
set the label's width constraint to a fixed value.
the details are as follows:
add a horizontal stackview into the cell's contentView
set the stackview constraints to fill the contentView with some margins:
eg. leading: 20, trailing 20, top 10, bottom 10.
add a label and a textField into the stackview.
in the stackview's Attribute inspector, set the spacing to 10.
set the label's width constraint to 100, or 0.3:1 to the contentView's width
so the label will have a fixed width, fixed spacing between label and textField, and the textField will take all the horizontal space remain.

Related

Horizontal Stackview in swift: how to wrap according content

I use a stackview (horizontal) with this setup:
Inside it, I have just 3 buttons (without text, only icon). The height/width button are set by constraints:
And the result is:
BUT I don't understand why my second button has a huge width... even if it width is fixed at 44.
Have you got some explanations guys please?
Thank you very much!
Your settings are just conflicting, that's the answer. It must fill stackview width with 3 buttons 44pt each + 6pt spacing between them, and this can't be resolved, because stackview width is bigger than 44*3 + 6*(3-1).
If you want all buttons be on the left with 6 point spacing, then remove trailing constraint for stackview;
if you want buttons to fill the full width -- then remove spacing = 6 and set distribution = equal spacing
Try to put the distribution to equal spacing on the stack view. That means that the spacing will be equal between every button. If you do not have any constrains on the stack view that affects the width then that should work.
Otherwise you need to increment the spacing value in order for the buttons to have equal widths.
Thanks for the question!

Random table view cell height is not working perfectly

I am designing a chat app in which I am using the UITableViewAutomaticDimension property of table view when I am setting the label lines to 6 then all cells height is according to the text which label contains but when I increase the number of lines for label then some cells have extra height. Even when scrolling it then cell height again changed.You can check the image so that you can understand it easily thanks.
You need to give label constraints from top and bottom, rather than center vertically.
Give vertical content hugging priority

Constraints for labels with changing size

Pic. 1
Pic. 2
Pic.1 - it is a structure of my ViewController. Label1 and Label2 have changing size (content comes from backend) for different users. If content of labels is small, I need to set Top Space constraint for TableView (vertical spacing) to Button (pic.1), but if content of labels is big, set Top Space constraint for TableView (vertical spacing) to Stack (pic.2). How to set constraints for TableView?
Embed your image,button two labels and stack in a view.
Then add following constraints
Top, leading and trailing to new container view
vertical space b/w container and table.
Required constraints for your image,button two labels and stack
Don't add bottom constraint for button.
Add bottom spacing to stack to its superview(embedded container) with a >=.
Height to container view with minimum height and set priority to low(250)

Dynamically sizing UITableViewCells

I have worked with dynamically sizing UITableViewCell before. But currently I am facing a problem statement where I am unable to achieve dynamic sizing using autolayout
My cell has three components viz-a-viz one UIImageView, two UILabel
The structure of the cell is as follows
UIImageView has to be of fixed width and height in my case 60X60
UIImageView will be vertically centered inside the UITableViewCell
UIImageView is having a leading constraint of 15 from the UITableViewCell
Now the two UILabels are vertically placed
Both the labels can have dynamic height and I want them to push expand the cell using autolayout but I am only able to achieve keeping the top UILabel of constant height and the below UILabel increases in height.
I want both the UILabel to expand vertically and increase the size of UITableViewCell.
Just use StackView for both label and make both labels numberOfLine to 0 and line break should be WordWrap. give leading,trailing,top and bottom constraints to stack view. for spacing between labels give spacing for stackview on xcode detail pannel.
See the Screenshots for the setting of the constrains.
Add a height constrain to the label with the constant greaterthanEqualTo a fixed value (see screenshot 2). and set the lines to 0. In the table view heightForRowAt indexPath set that to UITableViewAutomaticDimension .

Expanding UITextView inside a Stack View with scrolling enabled

I am trying to achieve something with Auto Layout and Stackview. I have a Vertical Stackview with a UIView, UITextView, and UIView in it as shown below.
I have checked out the previous answers here but couldn't find a clean solution to achieve this.
The UITextView is editable and must expand as the user types in it - for this I have disabled the scroll for the UITextView in the IB. I also have a height constraint set on the UITextView set to "Greater than or equal to" 10 and number of lines set to 0, so that the UITextView takes the intrinsic height at run time while the user types. I also want the expansion of the UITextView to continue until the UIStackView bottom edge reaches the Keypad accessory's top edge. I have been somewhat able to achieve the expanding UITextView part with my stack view pinned to the top, trailing, and leading edges of the Superview but I keep getting an error that the stackview needs Y position or height which is understandable but if I give it a fixed height or bottom constraint then the UITextView simply won't expand.
I also am not sure how to stop the expansion of the UITextView when it reaches the top edge of the Keyboard accessory view.
The code that I have so far is
let allowedHeight = self.view.frame.height - (self.keyboardHeight! + self.accessory.frame.height)
Basically find the allowed height in the view frame by subtracting the keyboardheight+Accessory view height. This calculation happens correctly. Next I do this (inside textViewDidChange) in hopes that just enabling/disabling the scroll on the UITextView would work but it clearly doens't since the whole text view then weirdly jumps up and down with every key stroke.
func textViewDidChange(_ textView: UITextView) {
if stackView.frame.height >= allowedHeight{
textView.isScrollEnabled = true
}
if stackView.frame.height < allowedHeight{
textView.isScrollEnabled = false
}
}
What is the best way to achieve what I am looking to do?
Couple notes...
I think you'll be better off setting the Max Height of your UITextView rather than of your UIStackView. The Text View will expand / contract based on its content, so you can set a <= height constraint.
Calculate the "elementsHeight" - the vertical size used by your top and bottom views, plus any spacing - and set the "max text view height" to the view height minus elementsHeight ... and subtract keyboard height when visible.
Update the text view's height constraint constant when "max text view height" changes.
Then set up an Observer for the text view's .contentSize ... and enable / disable scrolling based on .contentSize.height compared to "max text view height".
Needs a bit of hoop-jumping, to make sure you update sizes when subviews are laid-out, and to make sure you don't get in a recursion loop.
This is how I set it up:
The initial height constraint on the text view is <= 40 - but that doesn't really matter, as it will be changed via code every time the views layout differently.
I put up an example on GitHub -- it works, but is really just a starting-point. Take a look if you're interested, and pick it apart. https://github.com/DonMag/ExpandingTextView