Xcode 8, XIBs and Auto layout - UI gone for toss

Hi,


We use XIBs througout our project and we have lots and lots of them. As Xcode 8 became publicly available we downloaded the Xcode 8 from App store and udpated our development environments. Now when I open any XIB it asks me "Choose an initial device view" and I started choosing "iPhone 6s plus" as the device. The XIBs are now resized to a particular device size and all the controls inside it are also resized accordingly because all of them are controlled via autolayout. Everything looks good in XIB. Now when I run the application (simulator or device) and go to the particular view couple of controls do not appear at all on screen. If I break the constraint in XIB by moving them 1 point from where they are supposed to be and then run the application, they load without any errors. I tried everything that was possible in XIB including deleting and adding all the constraints but I am still seeing the same problem. If I revert the XIB to Xcode 7.x compatibility then it works fine. But with Xcode 8 it doesnt allow me to make changes to XIB which is compatible with Xcode 7.x.

I have attached the below screenshots at https://www.dropbox.com/sh/rpq6tdgja8fcvaj/AADYD9T0nSmFXy2YyOICo5p7a?dl=0


ChooseInitialDevice.png: Prompt to choose initial device view for XIB

Screenshot1-Working.png: Working Screen before XIB was converted to Xcode 8.x compatible.

Screenshot2-XIB.png: XIB converted to Xcode 8.x and all constraints are in place.

Screenshot3-Not-Working.png: Screen missing the Profile Image after the XIB was converted to Xcode 8.x compatible.

Screenshot4-Constraint-Break.png: Breaking the constraint by 1 point (move image view to x = 21 instead of x=20) and everything works fine when the application runs on simulator or device.


I understand the problem might be hard to understand and difficult to believe. If the images are not clear I can post a video for the same. I am seeing the same behavior for all the XIBs that are coverted to Xcode 8.x.

I have also submitted a bug with Apple: 28342777

I have the exact same problem. After opening my storyboard in Xcode8 and accepting the "Choose an initial device view" Dialog, everyting is acting strange. When I update all frames, the IB looks good, but after building, the app is totally broken. Tested it with 4 apps.

Please open a bug with apple. Every bug counts.

Closely following this thread as well - moving UI components in the Storyboard so that the Storyboard frame DOESN'T match the constraints seems to make components appear when run in app. Updating constraints, turning Auto Layout On/Off, moving the component + updating its frame all doesn't seem to work. I've downgraded to 7.3.1 in the meantime to actually be able to work.

Response from Apple in bug report: "We need more information to investigate this issue. Thank you for reporting the issue. Could we get more information about the profile image view? In Xcode 8, a fully constraint, non-misplaced view no longer saves out a frame to minimize diffs and support automatically update frames in IB. At runtime, these views get decoded with a placeholder size of 1000x1000, but are resolved after first layout. Could the image be assigned before initial layout, and would assigning the image to the image view after first layout address this case? Please send a sample to help us further analyze. thanks! Please provide your response or results by updating your bug report" I provided them a sample. Regards, Bhavik

This fix works for me. Just one single line of code 🙂
http://stackoverflow.com/a/39548367/5536251

Any luck with the 8.1 Xcode beta?

This is definitely not only related to Image views. We have issues with 3 Views: 2 Buttons and 1 normal UIView. All of them are 1000x1000 by the time viewDidLoad runs and the explicit height/width contstraints don't work at all / do wired things when loading the view.

No, Issue still present as the folks at Apple still investigating.

I'm having the same issue I upgraded my project using xCode 8 from xCode 7. Also, I only did the Swift 2.3 conversion because 3.0 I needed to slot more time for. Now when I open a storyboard it tells me to "choose a device version". If I try to set the option "open as xCode 7" it closes my storyboard and when I open it again. I'm mandated to "Choose a device version". Then when I run the app my tableView's are blank.

Same. Selecting a layout default ruined all my dynamic label placement, and i now have wierd transition animations between when switching view controllers. The Xcode 8 storyboard file wont run in the simulator *at all* after conversion without being completely visually messed up and missing 80% of the elements. I have to revert to xcode 7 storyboard compatability and modify the xml directly to make changes. This is completely insane.

I have an update:


When instantiating a uiview via XIB files (whether loading them from a view controller, or from inside of a UIView itself), or just creating IBOutlet variables for UIViews that are in your storyboard viewcontroller, XCode is not loading UIViews from the storyboard with the proper size constraints. It is resetting the UIView frame bounds to 1000x1000, instead of whatever size they are set to in the storyboard. This obviously breaks everything in autolayout, and any subsequent subviews that might be loaded thereafter.


So to fix:


In every UIView element that is linked to a view controller from the storyboard, you need to call .layoutIfNeeded() on each one. For example, in my xibSetup routine:


view = loadViewFromNib()

view.frame = bounds

view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]

view.layoutIfNeeded() //needed to have proper bounds height and width


or, if you have an IBOutlet for a UIView of any kind (linked from the storyboard):


uiviewfromstoryboard.layoutIfNeeded()


etc.

Calling .layoutIfNeeded() will force the app to re-evaluate the size constraints, and it will set them properly. Hopefully this makes sense and helps somebody.

Please check against Xcode 8.1, beta 2 (8T46g):

Xcode 8.0 did not always restore view frames from storyboards and xibs when layouts were ambiguous. Xcode 8.1 fixes several of these issues. If you have encountered these issues, resolve the ambiguity in the Auto Layout issues and update frames. Xcode 8.1 will persist them correctly. (28221021, 28244619)

Xcode 8.1 beta 3 solves this issue for me.

Xcode 8, XIBs and Auto layout - UI gone for toss
 
 
Q