简体   繁体   中英

How to deactivate my constraints?

I have a UITextView created programmatically in swift and this is what I have in my viewDidLoad() for my controller.

let isPortrait = UIInterfaceOrientationIsPortrait(UIApplication.shared.statusBarOrientation)
if isPortrait {
    searchTextView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.7).isActive = true
} else {
    searchTextView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.1).isActive = true
}

This is how the searchTextView is setup

func setupView() {
    self.layer.cornerRadius = 8.0
    self.translatesAutoresizingMaskIntoConstraints = false
    self.textContainerInset = UIEdgeInsets(top: 12, left: 10, bottom: 0, right: 10)
    self.textContainer.maximumNumberOfLines = 1
    self.textContainer.lineBreakMode = .byTruncatingHead
    self.autocorrectionType = .no
    self.text = ""
    self.font = Constants.fonts().largeFont
    self.backgroundColor = UIColor.white
    self.adjustsFontForContentSizeCategory = true
    self.textColor = UIColor.black

    self.layer.borderColor = UIColor.gray.cgColor
    self.layer.borderWidth = 0.25

    // Need clipsToBounds = false to show shadow

    self.clipsToBounds = false
    self.layer.shadowRadius = 20.0
    self.layer.shadowColor = UIColor.gray.cgColor
    self.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
    self.layer.shadowOpacity = 0.5

    self.tintColor = Constants.colors().primaryLightColor

}

This gives searchTextView a different widthAnchor based on the device orientation. This works fine, however, later I check when the device orientation is going to change and remove and add the corresponding widthAnchors.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    sideMenuLauncher.dismissSideMenu(duration: 0)

    let toPortraitMode = self.view.frame.size.width > size.width
    searchTextView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.7).isActive = toPortraitMode
    searchTextView.widthAnchor.constraint(equalToConstant: size.width * 0.1).isActive = !toPortraitMode


    for constraint in view.constraints {
        print("\(constraint)\n")
    }

    searchTextView.updateConstraints()
    searchTextView.layoutIfNeeded()

}

But this does not actually deactivate any constraints, as it says in this console output:

(
"<NSLayoutConstraint:0x60c000896170 PickupApp.SearchBarView:0x7f7ee1016a00.width == 56.8   (active)>",
"<NSLayoutConstraint:0x604000c9c6b0 PickupApp.SearchBarView:0x7f7ee1016a00.width == 397.6   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x604000c9c6b0 PickupApp.SearchBarView:0x7f7ee1016a00.width == 397.6   (active)>

I can't figure out why these constraints are not being deactivated even though constraints are being added.

searchTextView.translatesAutoresizingMaskIntoConstraints = false

You need to store that constraint in order to deactivate it later on.

Take a global variable to store the constraint.

var cons:NSLayoutConstraint?

Change viewDidLoad() as follows to access same constraint later on:

override func viewDidLoad() {
    super.viewDidLoad()
    let isPortrait = UIInterfaceOrientationIsPortrait(UIApplication.shared.statusBarOrientation)
    if isPortrait {
        self.cons = searchTextView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.7)

    } else {
        self.cons = searchTextView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.1)
    }
    self.cons?.isActive = true
}

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 (ie, 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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM