-1

I have one UIView which is transparent. This View is called "mainView"

I want to add a subview to mainView of the same size. So, I initialize it in the mainView Class itself: https://i.stack.imgur.com/pz6Ql.png It looks like this: [https://i.stack.imgur.com/Y8MEr.png] And that's exactly what I want!

But let's say I remove this code and I want to initialize a subView in my UIViewController: https://i.stack.imgur.com/aCpWf.png I created an outlet of the mainView and initialize with the bounds of mainView a subView.

Unfortunately, my view now looks now like this: https://i.stack.imgur.com/stekd.png But you can see in the code above I set the frame correctly to the frames of the view mainView.bounds

Can you explain why the size of the new View (subView) does not stay the same as when I initialized it in the mainView class itself?

Harsha pps
  • 2,012
  • 2
  • 25
  • 35
T. Arslan
  • 1
  • 2
  • 2
    You need to [edit] your question to include your code as text, not as links to images. – rmaddy Jun 26 '18 at 19:15
  • Why? isn't it simpler with pictures of the code? – T. Arslan Jun 26 '18 at 19:18
  • No. Code as links to pictures can't be searched, they can't be referenced, they make the question much harder to read, and here's the best part - copying and pasting the actual code into your question is much simpler then taking screen shots and uploading the images. – rmaddy Jun 26 '18 at 19:20
  • First, never create / add subviews in `Draw()` -- that will be called many times, and you'll end up with many duplicate buttons. Second, use auto-layout and constraints instead of explicitly setting the frame... the view's frame is not set in `viewDidLoad()`, which is why it's not matching. Using auto-layout fixes that, plus "auto" changes it when the superview changes (such as rotating the device). – DonMag Jun 26 '18 at 19:20
  • @DonMag You said "the view's frame is not set in `viewDidLoad()`. But when i set the frames after the mainView loaded it still has the same "false" size. `@IBOutlet weak var mainView: MainView! { didSet { let newView = UIView(frame: mainView.bounds) newView.backgroundColor = UIColor.green mainView.addSubview(newView) } }` – T. Arslan Jun 26 '18 at 19:51
  • Did you add mainView as subview of ViewController's view? – Stefan Jun 26 '18 at 19:52
  • @Stefan No. I created the mainView in the Storyboard. – T. Arslan Jun 26 '18 at 19:55
  • I found the problem! Like @DonMag said: "the view's frame is not set." I coded it in the hints button (for testing purposes) and now it works. `@IBAction func hintsPressed(_ sender: UIButton) { let newView = UIView(frame: mainView.bounds) newView.backgroundColor = UIColor.green mainView.addSubview(newView) }` But it is weird. Because which frame has it taken when the mainView frame was not set? – T. Arslan Jun 26 '18 at 20:01
  • MainView frame will not be set in the view did load instead try to do this in layoutSubviews method. –  Jun 26 '18 at 20:10

2 Answers2

0

You will be much, much better off if you learn how to use auto-layout.

override func viewDidLoad() {
    super.viewDidLoad()

    // create the view
    let newView = UIView()
    // we want to use auto-layout
    newView.translatesAutoresizingMaskIntoConstraints = false
    // set the view's background color
    newView.backgroundColor = .green

    // add the new view to the "main" view
    view.addSubview(newView)

    // constrain top / bottom / leading / trailing anchors
    newView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    newView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    newView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    newView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

    // the newView will now have the same frame as the "main" view, and its
    // frame will auto-adjust if the main view changes (such as on device rotate)
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Thank you. You said "the view's frame is not set in 'viewDidLoad' where is it then set? – T. Arslan Jun 30 '18 at 07:37
  • Couple decent answers here: https://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle ... essentially, auto-layout can make more than one "pass" during the load/initialize/display phases. Also, think about when the view changes size? The frame cannot *possibly* be set to its "finished state" at viewDidLoad(), because rotating the device can take place any time while the app is running. Just for example. – DonMag Jun 30 '18 at 16:59
0

Many Uiview issue solved by using this line. clipsToBounds = true

Tipu
  • 19
  • 4