-2

I am wondering something about UIView, and adding subviews...

So say I add a subview to a baseLayerView as so:

  self.newBaseLayer.addSubview(self.usernameLabel)

The UIView hierarchy/structure will, correct me if I'm wrong, like this:

  • newBaseLayer

    • self.usernameLabel

And then later in the code, I add another subview:

  self.newBaseLayer.addSubview(self.dateLabel)

And finally I add again the username label:

  self.newBaseLayer.addSubview(self.usernameLabel)

Will the usernameLabel duplicate or replace the other like so:

  • newBaseLayer

    • self.usernameLabel
    • self.dateLabel
    • self.usernameLabel

or

  • newBaseLayer

    • self.dateLabel
    • self.usernameLabel
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Why not look at the resulting hierarchy yourself and see what result you are getting? That would be quicker than typing up this question. – rmaddy May 05 '19 at 18:05
  • BTW - what does this have to do with layers? This seems like it only deals with `UIView`. – rmaddy May 05 '19 at 18:06
  • If your code is complete - and yes, you aren't talking `CALayer` hierarchy (at least nothing in your code suggests that) - then *absolutely* is `usernameLayer` (confusing choice of names there) wasn't removed from the hierarchy, why *would* it not be there? Or are you talking about which view is **front**? –  May 05 '19 at 18:09
  • Let me explain... as @rmaddy is very good at, well, most everything. (1) Every `UIView` can have subviews, and each have their own ability to have subviews. That's the view hierarchy. (2) A `UIView` has several `UIKit` subclasses, from image views to collection views, to navigation views, So yes, they inherit from `UIView`. (3) Every *single* `UIView` has a `CALayer` for it. Just code it as `myView.layer`. (4) Every `CALayer` has the ability to have their own "layer" hierarchy, using the `addSublayer` method. Last comment coming up.... –  May 05 '19 at 18:16
  • Lets talk `CALayer` and their subclasses - which operate pretty much like `UIView` does with it's. You can have a hierarchy, `CAShapeLayer` and it can be tied to a `UIBezierPath`, and the "CA" piece means "core animation". Since you clearly coded in terms of `addSubview`, I figured you aren't talking about layers - but hopefully you can see where your naming convention of `newBaseLayer` for a `UIView` is confusing in terms of your question. Maybe somewhere in my three comments I clarified and answered your question. If not, please, be more specific and I'll happily answer/comment as best I can –  May 05 '19 at 18:21

1 Answers1

3

I guess we are talking about UIViews and not Layers here?!

In this case usernameLabel is just added once even if you call addSubview multiple times.

To avoid any ambiguity here some code:

let baseView = UIView()
let dateLabel = UILabel()
let usernameLabel = UILabel()

override func viewDidLoad() {
    super.viewDidLoad()

    baseView.tag = 1
    usernameLabel.tag = 2
    dateLabel.tag = 3

    self.view.addSubview(self.baseView)

    self.baseView.addSubview(self.usernameLabel)
    self.baseView.addSubview(self.dateLabel)
    self.baseView.addSubview(self.usernameLabel)

    self.traverseViewHierarchy(view: self.baseView, level: 0)

}

private func traverseViewHierarchy(view: UIView, level: Int) {
    for _ in 0...level {
        print (" | ", terminator: "")
    }
    print ("view: \(view.tag)")
    for view in view.subviews {
        self.traverseViewHierarchy(view: view, level: level + 1)
    }
}

This add some tags to the mention views and gives it out in the console:

| view: 1
|  | view: 3
|  | view: 2

As you can see usernameLabel is just added once to the view hierarchy.

Stephan Schlecht
  • 26,556
  • 1
  • 33
  • 47