Change height of one of multiple fields in stack view - swift

In my code I have a stack view that initially has 1 element PhoneNumberField. Another PhoneNumberFieldscan be added dynamically in the runtime:
#IBAction func addAlternatePhoneNumberAction(_ sender: UIButton) {
let alternateNumberView = PhoneNumberField()
...
phoneNumberStackView.addArrangedSubview(alternateNumberView)
}
This is what xib for PhoneNumberFieldlooks like:
The problem is that I would like to be able to dynamically hide the 'Name for other phone' field based on the content of 'Mobile combobox'. When I set the 'isHidden' parameter everything works as expected, the only problem is that the PhoneNumberField height stays the same. I would like it to shrink when the 'Name for other phone' field is hidden.
I tried doing it using the outlet for height constant for otherNumberNameField in the PhoneNumberField.swift file but the problem is that in that case all of the PhoneNumberFields in the stack view have the size of the first field.
What would be the correct solution for this?
edit: In addition to the answer below: I had to set the distribution for the phoneNumberStackView to equal spacing. Worked like a charm.

First, create StackView.
Don't set its height constraint, just set top, leading, trailing and bottom constraints.
Bottom constraint set equal to Error label top constraint.
Then set its distribution to Fill Equally.
Now put first two Views into one view and put this view together with OtherNumberField view to this StackView.
So now your hierarchy should look like this:
Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.

Related

How to set margin for cell in tableView?

I tried to set a margin for a certain cell in my TableView by using code as below:
cell.layoutMargin.left = 20
However, when I launched the application, it changes nothing on the appearance. Is there any way I could achieve this?
For margins to take effect in any UIView, any constraints must have the "Constrain to margins" checked. Otherwise, the margins will not change any subview's constraints on the superview.
If you are adding constraints with Swift, here is an example of adding constraints relative to margins from Apple. The key part is:
// Get the superview's layout
let margins = view.layoutMarginsGuide
// Pin the leading edge of myView to the margin's leading edge
myView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
Ensure that your view is being updated and that any changes you are making to UIView is from the main thread.
To give a more thorough answer, more data is needed on your current auto layout setup.
I found a solution or alternative for this problem after getting help from my colleague, what we did was creating an IBOutlet for the constraint (which I just discover). So in UITableviewCell class, i add this line of code :
#IBOutlet var boxLeadingContraint: NSLayoutConstraint!
And in UITableView I set the margin for the specified cell like this:
cell.boxLeadingContraint.constant = 10
Note: I did this on a UIImageView inside the cell not on the cell itself. Any feedback or advice will be appreciated.

how to remove a view from stackview and distribute the other fillequally

i have a stackview desing in storyboard with 5 view inside composed of 1 buttom, a small view with a label and a button. like this.
what i want is to remove one of the the view let say the one with orange background.
i tried this on viewdidload
stackview.view2.isHiden = true
stackview.view2.removeFromSuperview()
this remove the view and all elements but the remaing does not distribute as expected any idea how to achive this
// Appears to remove the first arranged view from the stack.
// The view is still inside the stack, it's just no longer visible, and no longer contributes to the layout.
let firstView = stackView.arrangedSubviews[0]
firstView.isHidden = true

Table View don't bounce

My question is :
Why when I build and run the Table View created with different rows don't bounce, although I selected the checkbox Bounces and Bounce Vertically in Xcode 6 ?
By default UIScrollView, which is a superclass of UITableView won't bounce if the content fits within its bounds i.e. if the rows fit within table's bounds.
To alter this behaviour you can set alwaysBounceVertical property to true.
var alwaysBounceVertical: Bool { get set }
If this property is set to true and bounces is true, vertical dragging
is allowed even if the content is smaller than the bounds of the
scroll view. The default value is false.
Try this code:
[eventTable setBounces:NO];
(OR)In Storyboard untick the Bounces.

How to make horizontal UICollectionView with Swift

I want to display a list of product descriptions inside collection view cells and I'm using dequee for that.
I want to make sure that it only scrolls horizontally and not vertically at all.
The cell's width is 180px and the total number of my items is different every time.
I have tried a lot but the scrollView doesn't extend it's width at all.
You should specify the layoutDirection property of your collection in viewDidLoad:
if let flow = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout{
flow.scrollDirection = .Horizontal
}
or you can do this in Storyboard.
For swift 5 and easy way to do it. Use auto layout

UITableView does not return to the right place after a bounce when using custom section height

I need to set custom section height for my table view, and I'm trying to do it in pretty straightforward way by calling some delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 39.0;
}
I do get desired result - a taller section but I'v noticed one problem. When I tap the table (start dragging), move my finger up to top, and release the table, then the table bounces back but does not return to the correct initial place. If What I mean is the section header overlaps the first row. Here is the screen:
Any ideas why this happens or what workarounds exist?
UPDATE: I also tried increasing section height property of the tableView in IB. This increases the height but the same problem exist.
It sounds like you're dragging the table up so that the last row of the table, plus empty space below it, is showing, like this:
When you let go, it slides the table back down so that bottom edge of the last table row is flush against the bottom edge of the view.
This is the expected behavior for a table view. You're seeing the top row slip partly underneath your section header because the view height, minus the section header height, isn't an integer multiple of the row height. If you don't like this, you need to make sure the view height minus the section header height is an integer multiple of the row height.
There is another way to set the section height without using the delegate:
self.tableView.sectionHeaderHeight = 39.0;
If this does not help, you will have to fiddle with the UIScrollView Delegate.
Also, from your screen shot I see that you might be interested in the property tableHeaderView, which is a header above the table that will scroll off-screen when the table view is scrolled.