About the movement of removeFromSuperview()

Asked 2 years ago, Updated 2 years ago, 90 views

Generate a base View and addSubView() the UI parts that correspond to the feature.
After that, if I changed the function, I removeFromSuperview() all the UI parts on the base view, and then reset the value, addSubView().
After reconfiguration, the UI parts were lumped in the upper left corner of the screen, and when I checked with Debug View Hierarchy, I found the following error in RuntimeError:

Layout Issues 
   ->Position is ambivalent for UILabel

AutoLayout is configured on the StoryBoard.
Therefore, the first time you do not reconfigure it will be displayed correctly.

removeFromSuperview() I didn't expect the restriction to be lifted because I understood that the instance would not be released and only disconnected from View. Is there a possibility that the restriction on UI parts will be lifted?

swift xcode swift3

2022-09-30 11:51

3 Answers

Because AutoLayout represents a relative relationship with other adjacent views and superViews, removing the view using the removeFromSuperView() method usually results in inconsistent constraint errors such as those written.
If you change the structure, such as removing or adding views at runtime, you must also change the constraints.
Changing the AutoLayout constraints with code to match the view at runtime is troublesome, so if possible, it would be easier to have a different configuration of view and switch between views.


2022-09-30 11:51

See API Reference in removeFromSuperview().

Calling this method removes any constraints that refer to the view you are removing, or that refer to any view in the subtree of the view you are removing.

As stated, removeFromSuperview() removes the restrictions associated with the view as well.

Constraint errors occur because the constraints you set on the storyboard are lost.

You must reconfigure AutoLayout with the code, or save any constraints related to the view before calling removeFromSuperview() and reapply them after addSubview().


2022-09-30 11:51

removeFromSuperview() because I understood that the instance would not be released and only disconnected from View.

That perception is incorrect.A View is released if it is not strongly referenced by a non-Superview object.And Auto Layout constraints do not correspond to this 」non-Superview object には
Because "release" is used here to mean to be released and not released immediately, checking nil at a limited time just before the object monitoring system releases may allow you to obtain addresses.

At times like this, it is recommended to create a verification program.
Create one UILabel and two UIButton.For instance label, configure Constraint on the Storyboard.

import UIKit

classViewController:UIViewController {

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad(){
        super.viewDidLoad()
    }

    @ IBAction func removeLabel(_sender:AnyObject){
        label.removeFromSuperview()
    }

    @ IBAction funcnilCheck(_sender:AnyObject){
        if label == nil {
            print("Label is nil")
        } else{
            print("Label is not nil")
        }
    }
}

Press the removeLabel() button, and then press the nilCheck() button.

output:Label is nil

View placed in Storyboard/Interface Builder does not removeFromSuperview().If you want to remove View from the display, under isHidden (hidden in Swift2 and earlier), choose how to hide it.If you hide it, Constraint is maintained.


2022-09-30 11:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.