1

I need a button to be installed in portrait orientation while another button is uninstalled in portrait orientation (any width, any height), while the reverse should occur in landscape orientation (any width, compact height). One of these buttons is part of a UIStackView and the other is part of a UIView.

When in portrait the button in the UIStackView should be installed, but the button inside the UIView should be uninstalled, with the reverse occurring as I mentioned earlier in landscape. Currently I'm checking the userInterfaceIdiom in my view controller for the storyboard, but would like to rely entirely on Interface Builder if possible.

When removing my code (that actually makes it behave correctly), and simply relying on Interface Builder for installing/uninstalling the buttons based on size class, the button installed for landscape orientation always lays out correctly, but when rotating back to portrait, the other button that should now be installed is completely misplaced, out of it's original UIStackView, and that UIStackView that it should stay in, but hasn't, is now expanded to fill in the missing space where that button should have been. When this is happening, there are no warnings or errors from UIKit in the log, and my storyboard shows no conflicts. I'm thoroughly stumped!

At first glance this seems like an easy solution. Simply check/uncheck the install box for size classes of a UI element. Although, even after reading Apple docs and tutorials this is proving to be more difficult and obscure than I expected.

To clarify, I do not have constraints set for the button installed for portrait orientation since it is inside a UIStackView. On the other hand, I do have a few constraints set for the landscape orientation button. Although, the button in the UIStackView is the only one giving me trouble.

The view debugger is not working either in Xcode 7, and I'm receiving the error: warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available. when attempting to use it.

I believe this may be a bug and related to this post: UIStackView properties according to size classes

Your help is greatly appreciated! Thanks.

Community
  • 1
  • 1
  • "When this is happening, there are no warnings or errors from UIKit in the log" But do you have any auto layout ambiguities? You get no warning for that, but they can cause this sort of issue. You'll have to use logging / view debugging to find out. – matt Jun 16 '16 at 14:08
  • @matt Thanks for your quick response! IB is not showing any warnings or ambiguities for auto layout based constraints and/or frames for my storyboard. Is that what you were asking? – logan.blevins Jun 16 '16 at 14:13
  • No, I'm talking about what happens at runtime. – matt Jun 16 '16 at 15:52
  • @matt I do not receive these at runtime either. Any suggestions? – logan.blevins Jun 16 '16 at 16:53
  • I'd say, go back to doing this in code and move on. IB is evidently not capable of doing what you want. No biggie. – matt Jun 16 '16 at 17:56

1 Answers1

0

If you want a component to appear just in a specific size class this is what you do:

Compact x Regular

Add the constraints to you component using the size class you want it to appear in (wCompact hRegular for all iPhone in portrait and wAny hCompact for all iPhones in landscape). Next make sure that only that size class is ticked in the Attribute Inspector.

enter image description here

Leaving Installed ticked will make the component appear in any size class. You can change the size class of a view controller in a storyboard file by clicking on the option in the center bottom of the screen (see the blue bar at the bottom of the screen, where it says wAny hCompact in the image above).

I hope this helps!

UPDATE

Make sure that the constraints of the components are also set for the correct size class.

Constraint size classes

From the Document Outline select the constraints of the component and check the size class of the constraint in the Size Inspector. Usually, when you add constraints to a component with a specific Size Class selected in the story board, the constraints will automatically be added for that size class.

Community
  • 1
  • 1
Lucas
  • 880
  • 7
  • 12
  • I already installed/uninstalled the elements need for specific size classes. Like I said, they're properly installing/uninstalling but not always being 'laid out' correctly back in their original positions or containing views. This is happening even if I install/uninstall a set of constraints for the landscape button, whereas there are no constraints for the portrait button since it's residing within a `UIStackView`. – logan.blevins Jun 16 '16 at 14:15
  • @logan2995 make sure that your adding the constraints for the component in the correct size class too, not only *"installing"* it in the correct size class. See the Size Inspector for the constraints of the component. I've updated my answer with a screen shot of how to do this. – Lucas Jun 16 '16 at 14:23
  • The odd thing is that the button with constraints, which is only installed for landscape orientation, is working correctly, but the button installed for portrait orientation (which has no constraints since it's in a `UIStackView`) is the one not working. It shouldn't even be coming out of it's `UIStackView`. I suspect that the `UIStackView` is laying out it's other views and consuming the space before the button is becoming injected in the view hierarchy. – logan.blevins Jun 16 '16 at 14:27
  • @logan2995 the button in the UIStackView, should it appear or disappear in landscape? I'll update my answer with UIStackView Size Class definition. – Lucas Jun 16 '16 at 14:33
  • The button in the `UIStackView` should only be installed in portrait. It's properly installing/uninstalling according to size class, although the button completely misplaces itself outside of the `UIStackView` upon rotation back to portrait to the top left hand side of another view. – logan.blevins Jun 16 '16 at 14:41
  • @logan2995 is your button properly placed inside the `UIStackView`? What is the Alignment and Distribution setting for your UIStackView? Does the `UIButton` that's inside the stack view have any constraints? What kind of layout are you trying to achieve? I think your issue can be resolved using the `UIStackView` attributes inspector. – Lucas Jun 17 '16 at 17:01
  • there's no issue with the stack view attributes. Like I described in the original question, it works perfectly if I programmatically hide the buttons rather than installing/uinstalling in IB. – logan.blevins Jun 22 '16 at 20:21