Programmatically disable auto-layout constraint
When developing for iOS 8.0 or later, just use isActive
property of NSLayoutConstraint
after creating your IBOutlet.
UPDATED
- to have strong reference to the outlet per below suggestion, thank you @rob mayoff.
- to use .isActive instead of .active with Swift 4 per below suggestion, thank you @Mohit Singh.
your cell would have the following outlet:
@IBOutlet var photoBottomConstraint: NSLayoutConstraint!
and you would access the constraint in willDisplayCell
like:
myCell.photoBottomConstraint.isActive = false
and when you need it again:
myCell.photoBottomConstraint.isActive = true
How to disable a constraint programmatically?
The constraints have a boolean property called active
so if you want to disable a constraint you can easily call it:
constraint_A.active = false
How to deactivate my constraints?
I hope the following code will help you,
1) declare the global variables
var searchTextViewPortraitWidthConstraint: NSLayoutConstraint?
var searchTextViewLandscapeWidthConstraint: NSLayoutConstraint?
let searchTextView = UITextView()
2) viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupSearchTextView()
let isPortrait = UIInterfaceOrientationIsPortrait(UIApplication.shared.statusBarOrientation)
if isPortrait {
searchTextViewPortraitWidthConstraint = searchTextView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.7)
searchTextViewLandscapeWidthConstraint = searchTextView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.1)
searchTextViewPortraitWidthConstraint?.isActive = true
} else {
searchTextViewLandscapeWidthConstraint = searchTextView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.1)
searchTextViewPortraitWidthConstraint = searchTextView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.7)
searchTextViewLandscapeWidthConstraint?.isActive = true
}
}
configure textview
func setupSearchTextView(){
searchTextView.frame = CGRect(x: <#T##CGFloat#>, y: <#T##CGFloat#>, width: <#T##CGFloat#>, height: <#T##CGFloat#>)
/*
.
.
.
.
.
.*/
}
3) to activate or deactivate the width constraint based on device orientation use the any one of the below approach (i.e, viewWillLayoutSubviews()
or viewDidLayoutSubviews()
).
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let isPortrait = UIInterfaceOrientationIsPortrait(UIApplication.shared.statusBarOrientation)
if isPortrait{
searchTextViewPortraitWidthConstraint?.isActive = true
searchTextViewLandscapeWidthConstraint?.isActive = false
}else{
searchTextViewPortraitWidthConstraint?.isActive = false
searchTextViewLandscapeWidthConstraint?.isActive = true
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let isPortrait = UIInterfaceOrientationIsPortrait(UIApplication.shared.statusBarOrientation)
if isPortrait{
searchTextViewPortraitWidthConstraint?.isActive = true
searchTextViewLandscapeWidthConstraint?.isActive = false
}else{
searchTextViewPortraitWidthConstraint?.isActive = false
searchTextViewLandscapeWidthConstraint?.isActive = true
}
}
Removing/Adding constraint programmatically in ConstraintLayout
I have not worked through your code, but the following illustrates how to break and make the constraint using ConstraintSet
.
ConstraintSet set = new ConstraintSet();
ConstraintLayout layout;
layout = (ConstraintLayout) findViewById(R.id.layout);
set.clone(layout);
// The following breaks the connection.
set.clear(R.id.bottomText, ConstraintSet.TOP);
// Comment out line above and uncomment line below to make the connection.
// set.connect(R.id.bottomText, ConstraintSet.TOP, R.id.imageView, ConstraintSet.BOTTOM, 0);
set.applyTo(layout);
How to properly enable and disable constraint in one view controller on button click
Problem is each time you click the button 1 of the cases fire which eventualy call setupInputFields
and cause re-addition of new constraints which may conflict with old ones , so either access old constraints and delete them or create 2 arrays of possible variations and play with activate/deactivate
Removing constraints that were added programatically
You should assign your constraint to a property in your ViewController
. And then set .isActive
to false
instead of true.
Your code should look like this:
let myConstraint = firstView.topAnchor.constraint(equalTo: secondView.bottomAnchor, constant: 15)
Now, to activate it:
myConstraint.isActive = true
And to disable it:
myConstraint.isActive = false
How to remove constraints programmatically that is added from storyboard?
As @Henit mentioned, you can set IBOutlet for constraints as well.
For example,
@property(weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
so now, you can remove this constraint like this:
[myView removeConstraint: viewHeight];
Or else if you want to remove all / multiple constraints related to your view then,
[myView removeConstraints: constraintsArrayHere]; // custom array of constraints references
[myView removeConstraints: [myView constraints]]; //all constraints
Then later you can add your new constraints in the same manner using addConstraint
or addConstraints
method.
For more details go through Apple Documentation here.
Hope this helps.
Related Topics
How to Load My Own Reality Composer Scene into Realitykit
Function That Takes a Protocol and a Conforming Class (!) Instance as Parameters
Extend Generic Array<T> to Adopt Protocol
Safe to Signal Semaphore Before Deinitialization Just in Case
Convert Hex-Encoded String to String
Change Webview Url from Appdelegate
Cannot Add Alamofire to Swift Project
Why Is Swift's Ternary Operator So Picky About Whitespace
How to Assign an Array to a Class Property by Reference Rather Than a Copy
Cannot Convert Value of Type '()' to Specified Type Bool
How to Query Nested Data in Firebase Database
Swift 5 Table View Cell with Uiimage Appears Very Tall and Image Extremely Zoomed
Difference Between Dispatchsourcetimer, Timer and Asyncafter
Swift: How to Reload Row Height in Uitableviewcell Without Reloading Data
Place Activity Indicator Over Uitable View
Gradient as Foreground Color of Text in Swiftui
Programmatically Disabling Screenshot in App
Swift Extension on Generic Struct Based on Properties of Type T