4

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.

Dave G
  • 12,042
  • 7
  • 57
  • 83
  • I would rearrange anything that underlaps the boxes above and then back behind them. There is an annoying bug in Xcode where the arrangement of items in the storyboard is not how the simulator sees the order. Manually dragging things back and forth fixes it though. – Sethmr Aug 18 '16 at 17:01
  • @Sethmr Thanks, I tried it out but to no avail. However I'm not certain that the constraints are fine because I can see them working in preview assistant. I've tried restarting Xcode twice too. – Dave G Aug 18 '16 at 18:26
  • Make sure that your constraints are correctly installed on whatever context they are in. If you scroll to the bottom of the attributes inspector, you will see a checkbox with installed and the abbreviations of any possible types of contexts they will be laid out in. Make sure that box is checked for the objects that aren't showing themselves, and then do the same with each constraint attached to them. – Sethmr Aug 18 '16 at 18:30
  • @Sethmr Any ideas if I just commented out my roundView function and suddenly the view is visible? The roundView function only has two lines: viewToRound.layer.cornerRadius = (viewToRound.frame.width/2) viewToRound.clipsToBounds = true – Dave G Aug 18 '16 at 19:44
  • I do know that using the viewToRound.layer.cornerRadius = (viewToRound.frame.width/2) will through incorrect values at the cornerRadius if it is implemented before viewDidAppear finishes or before awakeFromNib() depending on how you initiate the View. It will base the width on the storyboard value, so it is normally wrong unless you have the width at a constant value. clipsToBounds I am not 100% sure of everything it does, but that definitely can have effects like making something disappear. I find reasons to use it, but it is always a trial and error if it does something I like, and seems to – Sethmr Aug 18 '16 at 21:35
  • have slightly different functionality based on the object that it is associated with. – Sethmr Aug 18 '16 at 21:36

4 Answers4

3

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.

Dave G
  • 12,042
  • 7
  • 57
  • 83
  • 1
    Apple never stop amazing me on how they can make developers life a big hell on EVERY update. – Ramon Canales Sep 18 '16 at 15:09
  • This actually happens because in the viewDidLoad and viewWillAppear methods the frames of the views are always {0, 0, 1000, 1000} Read more here http://stackoverflow.com/questions/39578530/since-xcode-8-and-ios10-views-are-not-sized-properly-on-viewdidlayoutsubviews – Paul Razvan Berg Oct 12 '16 at 08:41
1

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.

Christopher Rung
  • 436
  • 4
  • 11
  • This didn't fix it, but it helped. I hadn't checked it in preview assistant yet. In preview assistant it looks fine. I tried following the advice in the comment of @Sethmr but that didn't do it. – Dave G Aug 18 '16 at 18:21
0

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.

Tuslareb
  • 1,200
  • 14
  • 21
0

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.

Kex
  • 776
  • 10
  • 22