Is it possible to change the bottom layout guide y position? - ios

I would like to move the content of the whole screen up to make room for a view that I would add to the main window. Is it possible to adjust the bottom layout guide y coordinate, and make the whole view structure move up?
thanks.
ios8+

You can create an outlet to the bottom constraint of the view, and change its value from your code. UIView animations works fine in this case :
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.verticalBottomConstraint.constant = 44
)
Yep, the constant property of a NSLayoutConstraint is variable while the multiplier property is constant. Logic everywhere.

Related

Swift: Views not adjusting to programmatic constraints

I have a view that I am making hidden at the bottom of the screen, and want the scrollView above it to adjust and fill the void space.
The view at the bottom of the screen is a GADBannerView and has a fixed height of 50 (bannerHeight). The scroll view above it has a constraint to the bottom of the container that equals 50 (scrollConstraint). See photo.
In viewDidLoad is am setting these constraints to the following:
bannerHeight.constant = 0
scrollConstraint.constant = 0
This is causing the bannerView did disappear but the scroll view is staying in it's original position and not filling the void space.
You can force the superview to take into account the change of the constraint because this does not happen automatically. Add your code to viewDidLayoutSubviews() instead or simply call view.layoutIfNeeded() after you set the constants to 0 in the viewDidLoad().
If this does not work, you can try this alternative approach:
Go to your Storyboard and click on the scroll view's bottom constraint (the blue line that gives the scroll view its bottom constraint of 50). In the Attributes Inspector you should be able to see details about your constraint, it should look something like this
In the field that asks for an Identifier, give it the name "ScrollViewBottom" or whatever name you like.
Now loop over all the constraints that make up your scroll view and use the identifier name to find the correct one and change it's constant as follows
for constraint in yourScrollView.superview!.constraints {
if constraint.identifier == "ScrollViewBottom" {
constraint.constant = 0
}
}
Finally, force the view to take into account of this change by calling the following straight after
view.layoutIfNeeded()

Swift 3: Get distance of button to bottom of screen

I want to get the distance of a button to the bottom of the screen in Swift 3.
I can see the correct value in the Storyboard when I look at the distance (alt key) but unfortunately I am not able to calculate it manually.
The value I am looking for is the same as the constant in the "Vertical Spacing to Bottom Layout" constraint for the button.
view.frame.maxY - randomButton.frame.maxY
gave me a value way too high.
view.frame.size.height - (button.frame.size.height + button.frame.origin.y)
I think its ok! Hope it helps
If your button is not a direct successor of the view controller's view (aka, the hierarchy is something like ViewController's View -> SomeOtherView->Button), you won't get the right math by simply using frames. You will have to translate the button's Y-position to the coordinate space of the window object or your view controller's main view.
Take a look at this question: Convert a UIView origin point to its Window coordinate system
let realOrigin = someView.convert(button.frame.origin, to: self.view)
Then apply the math suggested by Lucas Palaian.
let bottomSpace = view.frame.maxY - (button.frame.height + realOrigin.y)
Another work around, in case something really wild and wierd is going on there, is to drag and drop an outlet of the button's bottom constraint. (Select the constraint from the view hierarchy in Interface Builder, hold the control key and drag the constraint to your view controller.) Then in your code you can access the constant.
let bottomSpace = myButtonBottomConstraint.constant
Use this to have it from the bottom of the button to the bottom of the view (Tested):
view.frame.size.height - randomButton.frame.size.height/2 - randomButton.frame.origin.y
I needed to find the distance from the bottom edge of the UICollectionView
to the bottom edge of the screen.
This code works for me:
#IBOutlet weak var collectionView: UICollectionView!
private var bottomSpace = CGFloat()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
bottomSpace = UIScreen.main.bounds.height - collectionView.frame.maxY
}
This method is called several times it gives the correct result.

Animating constraint on a pair of views (or keeping content constraints within an animating view)

I'm coding in Swift 2.0 with Xcode.
I have a UITableView that has a constraint to the top of the view. It is also constrained to the other 3 edges of the view but only the top constraint varies.
A label is then paired with the table with top/bottom/left/right constraints so it exactly relates to the size and position of the tableview.
I am animating the table constraint with the following code.
self.view.layoutIfNeeded()
self.myTableViewTop.constant = 300
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.view.layoutIfNeeded()
}) { (completed) -> Void in
// process completion stuff here
}
I expected that the label that is tied to all dimensions of the table would resize in accordance to the animating table size but it doesn't seem to.
What happens is the label reverts immediately to its final size and waits for the animated table to catch up.
What am I missing here? Something to do with priorities? Or a different way to tie the 2 elements together? Or do I need to animate both at same time?
Thanks.

Updating constraints programmatically

Situation is like I have a view with four buttons
There comes a condition when button with text Search Community should be hidden and width of Options button should get increased
Using Constraints for the first time in my project, I am not getting how to achieve this. Set of constraints added on both these buttons would be clear from the following two images.
The problem I see with your setup is that you have this constraint between the 2 buttons of equal width. This is true in the first case but when you want to hide one of them, it is not anymore.
So, you will need to rethink your constraints a bit. Maybe instead of the equal width constraint, you could use a static width constraint one the first button(the left one, that you want to hide). Then the second one, will just have a horizontal space constraint to the first one, and a trailing space to the superView.
Then you make an outlet in the VC for the width constraint of the first button and when you want to hide it you do something like this:
self.searchButtonWidthConstraint.constant = 0
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.view.layoutIfNeeded() // if you want to animate the constraint change
})
Let me know if you have questions. Good luck and hope it works out!
Update
For the landscape use case, you could listen to the orientation change notification, to update the width constraint of the button
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
self.searchButtonWidthConstraint.constant = LANDSCAPE_WIDTH
UIView.animateWithDuration(duration, animations: { () -> Void in
self.view.layoutIfNeeded() // if you want to animate the constraint change
})
}
Add constraint to 1st Button
Leading Space To NewView(gray colored)
Bottom space to NewView
Top Space to Topbutton (blue colored in your 1st and 2nd image)
Width (from Bottom - Right portion )
Add Constraint to 2nd Button
Trailing Space to NEwView
Bottom space to NewView
Top Space to Topbutton (blue colored in your 1st and 2nd image)
Horizontal space FirstButton ( Search Community )
Width

How do I move an UI Object in swift that has its constraints set in a storyboard?

So I am trying to create a simple moving animation for my UIImageView.
Problem is I've had its constraints set in the storyview (3 constraints, top, left and right positions).
So I cannot just alter coordinates - my image doesn't move if I do so.
So I guess I have to alter my constraints first. But I cannot even get them in code.
println(myPicture.constraints())
This returns an empty array.
How do I animate an object that has its constraints set in the storyboard?
You can change the constant values of the constraints. I would make outlets for them. Something like this:
#IBOutlet var topConstraint: NSLayoutConstraint!
You can connect these in Storyboard just like any other outlet. It's easiest if you find the constraint in the left-side view hierarchy menu and drag there to make the connections. Then in your code:
topConstraint.constant = 50 // or whatever you want to set it to
That should do it. If you want to animate the movement, you could put it an an animation block. If the change isn't happening when you do this, try calling this afterwards:
UIView.animateWithDuration(0.2,
animations: { () -> Void in
self.topConstraint.constant = 50
self.topConstraint.layoutIfNeeded()
},
completion: nil)

Resources