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
Related
I have 3 view View A , View B and Table view where View A is optional.
i.e. Only authenticated person can interact with View A.
So i have hided the View A for unauthenticated user by
viewA.isHidden = true
But this view is still taking space. What i want is if View A is hidden, Allow View B and table view take view A Space.
what I want is show in below pic.
Well, you should have to add two constraint
vertical space from view B to View A with constant = 10 and priority = 999 (high)
top space from view B to safe area with constant = 10 and priority = 750 (low)
the constraint which has higher priority is considered first. Add outlet connection to the ViewController.
when you want to hide view A then change the priority of second constraint to 999(high) and first constraint to 750(low) programatically.
constaraint.priority = UILayoutPriority(rawValue: 750)
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.
I use the following code to change the background colour and border radius of a UITableView:
let containerView:UIView = UIView(frame:self.gamesTableView.frame)
gamesTableView.layer.cornerRadius = 5
gamesTableView.layer.masksToBounds = true
gamesTableView.backgroundColor = UIColor(red:1, green:1, blue:1, alpha:0.40)
self.view.addSubview(containerView)
containerView.addSubview(gamesTableView)
This worked fine in xcode 7 and produced the result I expected. However in Xcode 8 this code seems to cause the buttons on the screen not to work, using the debug view hierarchy tool I discovered that with this code there is an extra UIView created that seems to obstruct the buttons. Removing the code in question does solve the problem but it also takes away the extra traits I gave the table view. I would like to know whether there is an alternative method to achieve the same result. I have included images of the view hierarchy with and with out the code.
The buttons in question are: "Start new game" and "Settings"
Side view without styling code:
Side view with sling code, notice the highlighted layer in front:
The fix for this seems to be as simple as putting the code in the question above in viewDidLayoutSubviews
Like this:
override func viewDidLayoutSubviews(){
let containerView:UIView = UIView(frame:self.gamesTableView.frame)
gamesTableView.layer.cornerRadius = 5
gamesTableView.layer.masksToBounds = true
gamesTableView.backgroundColor = UIColor(red:1, green:1, blue:1, alpha:0.40)
self.view.addSubview(containerView)
containerView.addSubview(gamesTableView)
}
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
If while scrolling header section in UICollectionview rose above navigationitem how can I know the index of the header section which is not visible?
You can use view.window property if its nil it means its not visible.
Since Collection view re-uses views you would like to check which last header view is visible
instead and subtract one .
1) For the UIViews which are visible get the min index.
2) All the view above it are not visible.