Giter VIP home page Giter VIP logo

funwithsizeclasses's Introduction

FunWithSizeClasses

This small project to reproduce a problem I encounterd while working with Size Classes.

Context

There is a single view controller - ViewController.swift. A custom view is designed on a separate file CustomView.xib which gets added the view controller's view on viewDidLoad()

That view has:

  • One UIButton added for Any-Any size class.
  • One UIView added for Regular-Regular size class only.

To hide and show the UIView 2 constraints with their corresponding IBOutlet are added:

  1. One to align the Top of the button and the view - Identifier: HiddenConstraint
  2. One to align the Bottom of the view to the Top of the button - Identifier: VisibleConstraint

CustomView.swift contains a method - showDummyView(_:) to show and hide the view by activating and deactivating the above constraints.

Problem

Trying to activate or deactivate any of the above constraints on awakeFromNib() would produce the following crash:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with items <UIView: 0x7fa850d977c0; frame = (0 0; 600 600); autoresize = RM+BM; layer = <CALayer: 0x7fa850d94790>> and <UIButton: 0x7fa850d95240; frame = (0 549; 600 51); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7fa850d957b0>> because they have no common ancestor.  Does the constraint reference items in different view hierarchies?  That's illegal.'

Just run the project as is to see this. That could be kind of expected since the main view has not being added to the view hierarchy yet

Remove the comment on showDummyView(_:) and the same crash will be seeing this time when the call comes from viewWillAppear on the view controller.

What's going on?

The UIView is CustomView, the one added for Regular - Regular only, is not in the view hierarchy neither on awakeFromNib nor on viewWillAppear.

It seems that the traitCollection property of CustomView gets set to Regular - Regular a bit later than what I would expect. In particular:

  • In awakeFromNib() it is Unspecified - Unspecified which is kind of expected.
  • On viewWillAppear() and it is still Unspecified - Unspecified.
  • On viewDidAppear() it is Regular - Regular.

On the contrary, the view controller has the Regular - Regular traitCollection since viewDidLoad()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.