Dave
Dave

Reputation: 12216

Xcode 8 - Some Views & VCs Not Showing Up on Simulator

The app ran perfectly prior to updating to Xcode 8 Beta 6 and Swift 3. I've changed nothing else but now I have two problems.

First, a few random views are no longer showing up. They're just square, colored boxes. The views above them show up though.

In Interface Builder:

Views

On simulator:

Only locks on simulator

Second, my model VC is no longer appearing when segued. It did before and I can see the segue is being called but now its' not there.

If anyone can provide ideas about either problem it'd be greatly appreciated.

Upvotes: 4

Views: 2463

Answers (4)

Kex
Kex

Reputation: 776

There is an issue with iOS 10 UIView lifecycle & AutoLayout.

What I mean by that is that in methods such as viewDidLoad & viewWillAppear the frames on all UI elements are `{{0,0},{1000,1000}}.

In cases such as with setting round corners makes rounded corners with 500px & you get invisible UI components :)

How I resolved this issue is by setting the rounded corners in viewDidLayoutSubview in UIViewControllers or layoutSubviews in UIView subclasses.

Upvotes: 0

Tuslareb
Tuslareb

Reputation: 1200

It's definitely an AutoLayout issue in Xcode 8. The problem doesn't exist without using AutoLayout. I made this workaround using a protocol:

protocol Roundable {}

extension Roundable where Self: UIView{

    func roundCorners(){

        self.layer.cornerRadius = self.bounds.height / 2
    }
}

class CustomView: UIView, Roundable {

    override var bounds: CGRect {

        didSet{
            roundCorners()
        }
    }
}

Make the view involved a CustomView and it will show up rounded. I use a protocol here, because in this way it's easy to extend an already existing UIView subclass with the functionality to round the corners. Of course it is possible to set the cornerRadius directly in the bounds property observer.

Upvotes: 0

Dave
Dave

Reputation: 12216

So between Xcode 7/Swift 2 --> Xcode 8/Swift 3, something changed with how to turn a UIView into a circle. Here is my code now:

func roundView (_ viewToRound: UIView) {
    viewToRound.layer.cornerRadius = 20
    //viewToRound.layer.cornerRadius = (viewToRound.frame.width/2)
    viewToRound.clipsToBounds = true
}

As you can see, I've replaced my cornerRadius method to be an explicit "20" instead of inferred from the view size. With my previous "frame.width" the views were literally not showing up at all. Now they're back to normal. I don't know what changed but this definitely fixed it.

Upvotes: 3

Christopher Rung
Christopher Rung

Reputation: 436

Something may have happened to the auto layout constraints. Double check that those are set properly.

Also, you don't need to use the simulator to verify this; use the Assistant editor's Preview view:

preview view

As a sanity check, the first thing I would do is reset all of the elements in your view to the suggested constraints to see if that resolves the problem.

Upvotes: 1

Related Questions