Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 202 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Programmatically disable auto-layout constraint

#1
Is there any programatic way to temporarily disable an auto-layout constraint? I do not want that constraint to be considered for a certain period of time at all until I need it again.
Reply

#2
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
Reply

#3
Base on oyalhi's answer, also want to point out that you have to make a strong reference to your constraints if you want to make it inactive:

@IBOutlet var photoBottomConstraint: NSLayoutConstraint!

It is not abvious, but if you are using weak reference, `photoBottomConstraint` could be nil after this call:

myCell.photoBottomConstraint.active = false
Reply

#4
You use `NSView`'s `removeConstraint:`; if you've created the constraint in the interface builder you connect it to the code through an `IBOutlet`

class MyView : NSView {
@IBOutlet var temporaryConstraint : NSLayoutConstraint!

var constraint : NSLayoutConstraint! = nil /* my strong link */
var constraintShowing : Bool

func awakeFromNib() {
constraint = temporaryConstraint
}

func toggleLayoutConstraint(sender : AnyObject) -> () {
if constraintShowing {
self.removeConstraint( constraint )
} else {
self.addConstraint( constraint )
}
constraintShowing = !constraintShowing
}
}

Sort of like the dance we used to have to do with `NSTableColumns` in the 10.4 days before they could be hidden.

---

You can also do a little controller gadget

class ConstraintController {
var constraint : NSLayoutConstraint
var view : NSView
var show : Bool {
didSet {
if show {
view.addConstraint(constraint)
} else {
view.removeConstraint(constraint)
}
}
}

init (c : NSLayoutConstraint, inView : NSView) {
constraint = c
view = inView
show = true
}
}

class MyView : NSView {
@IBOutlet var temporaryConstraint : NSLayoutConstraint!
var control : ConstraintController? = nil

func awakeFromNib() -> () {
control = ConstraintController(temporaryConstraint, inView: self)
}

func show(sender : AnyObject!) -> () {
control!.show
}

func hide(sender : AnyObject!) -> () {
control!.hide
}
}

More lines but arguably easier to understand and less hackish.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through