Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 8 sets frame size differently. How do I find more details?

Tags:

uikit

xcode8

In several places in my application I get zero frame sizes where I used to get a frame. This impacted me especially when creating round objects by doing a cornerRadius of size/2.

For example, in Xcode 7 this worked fine:

class AvatarUIButton: UIButton {
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    layer.masksToBounds = true
    layer.cornerRadius = bounds.size.width / 2
  }
}

But now in Xcode 8 I have to do this:

class AvatarUIButton: UIButton {
  override var bounds: CGRect { didSet {
    layer.cornerRadius = bounds.size.width / 2
  }}

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.layer.masksToBounds = true
  }
}

In this example, the change is arguably better/more obvious. I have another situation that is much less isolated involving a 0 frame on TableHeaderView that is only for xcode 8.

I'm looking for release notes, mailing list discussion, or similar that discusses a change in ordering of frame size determination so I can figure out what changed and how I can fix it.

like image 481
SimplGy Avatar asked Jul 21 '16 16:07

SimplGy


1 Answers

In Xcode 8, Apple say :

Xcode 8 removes the ability to configure views without constraints with frame varying per size class. Views without constraints may look different after compiling with Xcode 8 when switching size classes at runtime. Going forward, use constraints and autoresizing masks with the device bar to achieve the desired layout. (25894544)

In previous versions of Xcode, we used to define the design on the ViewDidLoad. But here, the different elements were not created (and constraints too), so the view was created with the size in the Storyboard.

Now, in Xcode 8, we cannot do it anymore. We need to have constraints because the frame is not initialize (that's why some of you have these values for the frame size : 0, 0, 1000, 1000). For example, make it in the ViewDidAppear and it will work fine. But, you will see at first your button without corner radius few times.

So, what you can do is to use this function (here in Objective C) (where you want, even in the loading function) :

[self performSelector:@selector(setCornerToMyButton) withObject:NULL afterDelay:0];

You can pu the delay you want, but even with 0 it works.

And change your radius in this function like this for example :

- (void) setCornerToMyButton {
    //Do what you want... 
    layer.masksToBounds = true
    layer.cornerRadius = bounds.size.width / 2
}

Hope this will help you.

like image 60
PMIW Avatar answered Sep 30 '22 17:09

PMIW