How to programmatically disable constraint when animating - swift

I am have a view with a fixed width anchored to the left. I want this view to animate (moving left to right) by anchoring it to the right (thus removing the left anchor). How do I do this?
I tried setting a priority but I am not sure of the syntax. I also tried to disable the constraint, but that did not work.
fileprivate func sparkle() {
let sparkleView = UIView()
sparkleView.backgroundColor = UIColor.yellow
sparkleView.alpha = 0.5
addSubview(sparkleView)
sparkleView.translatesAutoresizingMaskIntoConstraints = false
sparkleView.topAnchor.constraint(equalTo: topAnchor).isActive = true
sparkleView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
sparkleView.widthAnchor.constraint(equalToConstant: frame.width / 5).isActive = true
sparkleView.leftAnchor.constraint(equalTo: leftAnchor).priority = UILayoutPriorityDefaultLow // Doesn't work
sparkleView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
layoutIfNeeded()
sparkleView.leftAnchor.constraint(equalTo: leftAnchor).isActive = false // Doesn't work
sparkleView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
UIView.animate(withDuration: 2, animations: {
self.layoutIfNeeded()
}) { (_) in
sparkleView.removeFromSuperview()
}
}
Currently, the width constraint gets broken since we are assigning a left and right anchor.
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000020b8eb0 UIView:0x7fc846d551d0.width == 15.35 (active)>
-- EDIT --
I have tried creating an instance variable to hold the constraint.
let leftCons = sparkleView.leftAnchor.constraint(equalTo: leftAnchor)
leftCons.isActive = true
I then tried to modify the priority
leftCons.priority = UILayoutPriorityDefaultLow
However, this causes error
'Mutating a priority from required to not on an installed constraint (or vice-versa) is not supported. You passed priority 250 and the existing priority was 1000.'
Instead, I need to just set it as inactive (from the answers)
leftCons.active = false
-- Correct code if interested.. --
fileprivate func sparkle() {
let sparkleView = UIView()
sparkleView.backgroundColor = UIColor.yellow
sparkleView.alpha = 0.5
addSubview(sparkleView)
sparkleView.translatesAutoresizingMaskIntoConstraints = false
sparkleView.topAnchor.constraint(equalTo: topAnchor).isActive = true
sparkleView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
sparkleView.widthAnchor.constraint(equalToConstant: frame.width / 5).isActive = true
let leftCons = sparkleView.leftAnchor.constraint(equalTo: leftAnchor)
leftCons.isActive = true
layoutIfNeeded()
leftCons.isActive = false
sparkleView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
UIView.animate(withDuration: 0.5, animations: {
self.layoutIfNeeded()
}) { (_) in
sparkleView.removeFromSuperview()
}
}

You need to create a var
var leftCon:NSLayoutConstraint!
then alter it , you problem is every line create a new constraint
sparkleView.widthAnchor.constraint(equalToConstant: frame.width / 5).isActive = true
sparkleView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
sparkleView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
note: above 3 constraints create a conflict as the view can't be pinned to left and right at the same time with a width constraint that why you see the conflict break => NSLayoutConstraint:0x6000020b8eb0 UIView:0x7fc846d551d0.width == 15.35 (active)
regardless of the active assignment so those
sparkleView.leftAnchor.constraint(equalTo: leftAnchor).priority = UILayoutPriorityDefaultLow // Doesn't work
sparkleView.leftAnchor.constraint(equalTo: leftAnchor).isActive = false // Doesn't work
are on the fly and don't alter the created constraint , so to be
leftCon = sparkleView.leftAnchor.constraint(equalTo: leftAnchor)
leftCon.isActive = true
Regarding this crash
'Mutating a priority from required to not on an installed constraint (or vice-versa) is not supported
you need to set the priority before the activation
leftCon.priority = UILayoutPriorityDefaultLow
leftCon.isActive = true

Related

Why is my UIView not displaying correctly when updating autolayout constraints?

I am trying to animate a simple autolayout constraint change, but when I call it the first time it animates but instead of just stretching and changing the height, it moves the whole view up, if I call it again it then fixes itself.
Here is how I set up the constraints:
hiddenView.addSubview(topView)
topView.translatesAutoresizingMaskIntoConstraints = false
topView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
topView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
topView.bottomAnchor.constraint(equalTo: hiddenView.bottomAnchor).isActive = true
topViewDefaultTopAnchorConstraints.append(topView.topAnchor.constraint(equalTo: hiddenView.centerYAnchor))
topViewSelectedTopAnchorConstraints.append(topView.topAnchor.constraint(equalTo: hiddenView.topAnchor))
NSLayoutConstraint.activate(topViewDefaultTopAnchorConstraints)
And here is how I am updating them:
func showTopView() {
NSLayoutConstraint.deactivate(topViewDefaultTopAnchorConstraints)
NSLayoutConstraint.activate(topViewSelectedTopAnchorConstraints)
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
}
Update:
Here is a gif of what happens when calling showTopView, calling it again fixes the bottom constraint:
It should just animate up like in the second image, not bringing the whole view up, as the bottomAnchor does not change, how can I fix this?
Update: I realised that I am rounding the corners of topView and bottomView, if I don't round the top corners then it works correctly, so it has something to do with this.
override func viewDidLayoutSubviews() {
topView.roundCorners(corners: [.topLeft, .topRight], radius: 100*ScreenDimensions.ASPECT_RATIO_RESPECT_OF_XMAX)
bottomView.roundCorners(corners: [.topLeft, .topRight], radius: 100*ScreenDimensions.ASPECT_RATIO_RESPECT_OF_XMAX)
topConstraint = topView.topAnchor.constraint(equalTo: hiddenView.topAnchor, constant: hiddenView.frame.height/2)
topConstraint.isActive = true
}
Seems the hiddenView's bottomAnchor is pulled up along with the topView's bottom here. Make sure the hiddenView's bottomAnchor is constrained properly. And better way to do this would be to provide a heightAnchor and increase the heightConstraint constant value to animate the view to increased height.
I think I know what your issue is. You might need to play with the constant rather than having two different constraint defined that you activate/deactivate.
First, define a constraint a the top of your ViewController like so:
var topConstraint: NSLayoutConstraint!
Then, initialize it as shown below:
hiddenView.addSubview(topView)
topView.translatesAutoresizingMaskIntoConstraints = false
topView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
topView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
topView.bottomAnchor.constraint(equalTo: hiddenView.bottomAnchor).isActive = true
topConstraint = topView.topAnchor.constraint(equalTo: hiddenView.topAnchor, constant: hiddenView.frame.height/2)
topConstraint.isActive = true
Then inside showTopView, you only need to update the constant and that should create the animation you are looking for:
func showTopView() {
topConstraint.isActive = false
topConstraint.constant = 0
topConstraint.isActive = true
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
}
Similarly if you want to put the view back to normal, you'd implement it as follows:
func hideTopView() {
topConstraint.isActive = false
topConstraint.constant = hiddenView.frame.height/2
topConstraint.isActive = true
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
}
You may have better luck with this approach:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let r = 100*ScreenDimensions.ASPECT_RATIO_RESPECT_OF_XMAX
topView.layer.cornerRadius = r
topView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
bottomView.layer.cornerRadius = r
bottomView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
}

Add uibutton with Auto Layout programmatically

I want to add UIButton like this:
let switchTheme: UIButton = {
let button = UIButton.init()
button.backgroundColor = .red
button.setTitleColor(.blue, for: .normal)
button.setTitle(Settings.isLightTheme() ? Strings.Various.switchToDark.value : Strings.Various.switchToLight.value, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
And then set constraints like:
switchTheme.bottomAnchor.constraint(equalTo: view.bottomAnchor)
switchTheme.leftAnchor.constraint(equalTo: view.leftAnchor)
switchTheme.rightAnchor.constraint(equalTo: view.rightAnchor)
switchTheme.heightAnchor.constraint(equalToConstant: 40.0)
But it shown not on bottom as it suppose to but on top and without constraints applied.
You will need to set constraints activate state = true. You can do it simply,
NSLayoutConstraint.activate([
//Move your existing code HERE with comma separated
])
In case of any problem, you can check this following function:
func setConstraints() {
NSLayoutConstraint.activate([
switchTheme.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor), // bottomAnchor to set bottom target.
switchTheme.leftAnchor.constraint(equalTo: self.view.leftAnchor), // leftAnchor to set X left
switchTheme.rightAnchor.constraint(equalTo: self.view.rightAnchor), // rightAnchor to set X right
switchTheme.heightAnchor.constraint(equalToConstant: 40.0) //heightAnchor to set appropriate height.
])
}
You need to active those constraints just simple like this :
switchTheme.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
switchTheme.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
switchTheme.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
switchTheme.heightAnchor.constraint(equalToConstant: 40.0).isActive = true
Your constrains must be activated :
switchTheme.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

How to fix 'Unable to simultaneously satisfy constraints.' conflict between height constraint and center Y constraint

I want to vertically centre a UILabel in a UIView. I'm using the following constraints:
layoutGuide = safeAreaLayoutGuide
header.translatesAutoresizingMaskIntoConstraints = false
titleLabel.translatesAutoresizingMaskIntoConstraints = false
header.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
header.rightAnchor.constraint(equalTo: layoutGuide.rightAnchor).isActive = true
header.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
header.heightAnchor.constraint(equalToConstant: UI.HEADER_HT).isActive = true
titleLabel.topAnchor.constraint(equalTo: header.topAnchor, constant: SPACING.LG).isActive = true
titleLabel.rightAnchor.constraint(equalTo: header.rightAnchor, constant: -SPACING.LG).isActive = true
titleLabel.leftAnchor.constraint(equalTo: header.leftAnchor, constant: SPACING.LG).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: UI.SCREEN_TITLE_HT).isActive = true
titleLabel.centerYAnchor.constraint(equalTo: header.centerYAnchor).isActive = true
The label is correctly centred but the height constraints is not being respected
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000003ade50 FC.StyledLabel:0x7fa077845330.height == 40.6 (active)>
If you want the title vertically centered, you probably don't want both topAnchor and centerYAnchor constraints. Perhaps you want to anchor that header to the label's top, instead?
Also, check out NSLayoutConstraint.activate. It will allow you to get ride of all the isActive = trues.

Updating Constraints programmatically?

I have a subclass of UIView, I want to manipulate a constraint but it does not work.
When pressing a button exchangesViewActivated changes and the functions get called.
var exchangesViewActivated = false {
didSet {
if exchangesViewActivated == true {
setExchangesView()
} else {
setUpLayout()
}
}
}
Die subview and translatesAutoresizingMaskIntoConstraints are set.
func setUpLayout() {
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive = true
bottomContainer.leadingAnchor.constraint(equalTo: scrollViewContrainer.leadingAnchor).isActive = true
bottomContainer.trailingAnchor.constraint(equalTo: scrollViewContrainer.trailingAnchor).isActive = true
bottomContainer.topAnchor.constraint(equalTo: configBar.bottomAnchor).isActive = true
bottomContainer.bottomAnchor.constraint(equalTo: scrollViewContrainer.bottomAnchor).isActive = true
}
Now I want to manipulate the constraint by calling this function:
func setExchangesView() {
bottomContainer.bottomAnchor.constraint(equalTo: scrollViewContrainer.bottomAnchor).isActive = false
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive = false
bottomContainer.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
}
But this constraint stays activated:
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive
Am I missing something? Is it not enough to set a constraint to false to deactivate it? Do I have to call something else?
setUpLayout() is adding new constraints every time you call setUpLayout() method. You must save constraint reference and update it next time.
For example.
// class variable
var bottomConstraint:NSLayoutConstraint? = nil
bottomConstraint = bottomContainer.heightAnchor.constraint(equalToConstant: 500)
bottomConstraint.isActive = true
And later update constraint
bottomConstraint.constant = 100
bottomConstraint.isActive = true
This doesn't deactivate old constraints as it deactivates the newly created ones so do
var heightCon:NSLayoutConstraint!
var botCon:NSLayoutConstraint!
//
heightCon = bottomContainer.heightAnchor.constraint(equalToConstant: 500)
heightCon.isActive = true
botCon = bottomContainer.bottomAnchor.constraint(equalTo: scrollViewContrainer.bottomAnchor)
botCon.isActive = true
Then you can easily reference the constraints and deactivates them and add the new constraints

iOS-Charts - X Axis Labels Disappear when Scrolling

I've been working with iOS-Charts from Daniel Gindi and it is a GREAT Framework!
However, I've been struggling with this issue for a long time now. I'm using a BarChart and managed to display the correct Values # the Axis labels.
Now I Don't know why this happens but the labels seem to DISAPPEAR While I'm Scrolling...
Here is a simple example with 2 bars:
And now I Scroll a bit to the Right, and PUFFF! The label is Gone!
// HERE IS MY SETUP CODE:
func setupChartView() {
// Default Texts:
noDataText = ""
chartDescription?.text = ""
// xAxis:
xAxis.drawGridLinesEnabled = false
xAxis.drawAxisLineEnabled = false
xAxis.enabled = true
if (UIScreen.main.nativeBounds.height < iPhone6ScreenHeight) {
xAxis.yOffset = smallScreenYOffset
} else {
xAxis.yOffset = graphLabelYOffset
}
xAxis.labelPosition = .bottom
xAxis.labelFont = defaultTextFont!
xAxis.labelTextColor = .engieChartGrayColor()
xAxis.granularity = 1.0
// Left/Right Axis:
leftAxis.enabled = false
rightAxis.drawGridLinesEnabled = false
rightAxis.drawAxisLineEnabled = false
rightAxis.drawLabelsEnabled = false
rightAxis.drawZeroLineEnabled = false
// Other UI Vars:
dragEnabled = true
drawMarkers = false
drawBordersEnabled = false
clipValuesToContentEnabled = true
drawValueAboveBarEnabled = false
setScaleMinima(4.0, scaleY: 1.0)
legend.enabled = false
doubleTapToZoomEnabled = false
pinchZoomEnabled = false
resetZoom()
scaleXEnabled = false
scaleYEnabled = false
}
Can someone Explain to me why this is?
Thanks, I Really Appreciate the help...