7

I've been trying to create a container view controller which provides some overlaid views like UINavigationController and UITabBarController do for view controllers in iOS 7. To make the contained views layout properly I've tried just about everything I can think of with regards to implementing -bottomLayoutGuide in both the container and contained view controllers, but with no luck. The method is called, but the value does not seem to be used.

I've put together a quick example at https://github.com/stefanfisk/custom-layout-guides, but there I was not able to even get the accessors called.

Stefan Fisk
  • 1,563
  • 13
  • 19

1 Answers1

2

I've found that when you set up the constraints in code, e.g.

[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:[topLayoutGuide][mainView]"
                           options:0
                           metrics:nil
                           views:@{@"topLayoutGuide" : self.topLayoutGuide, @"mainView" : self.mainView}]];

it crashes with:

2013-10-16 22:23:27.119 Custom Layout Guides[46840:a0b] -[LayoutGuide superview]: unrecognized selector sent to instance 0x8c80c80
2013-10-16 22:23:27.124 Custom Layout Guides[46840:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LayoutGuide superview]: unrecognized selector sent to instance 0x8c80c80'

It's weird that Auto Layout tries to call superview on a layout guide, as it should only conform to the UILayoutSupport protocol.

I've also noticed that topLayoutGuide & bottomLayoutGuide are declared as readonly:

@property(nonatomic, readonly, retain) id<UILayoutSupport> topLayoutGuide
@property(nonatomic, readonly, retain) id<UILayoutSupport> bottomLayoutGuide
Arek Holko
  • 8,966
  • 4
  • 28
  • 46
  • I, it seems like they are not supposed to be overridden by subclasses, but to me the documentation reads as if the actual objects returned will be used by the layout engine, which implies that overriding it should work. The object returned by the framework implementations is of this class though, which is clearly a view: https://github.com/JaviSoto/iOS7-Runtime-Headers/blob/master/Frameworks/UIKit.framework/_UILayoutGuide.h – Stefan Fisk Oct 16 '13 at 22:55
  • @StefanFisk: it seems to me that in this case the documentation doesn't match the implementation. Maybe you can find some workaround that won't necessitate overwriting layout guides? – Arek Holko Oct 17 '13 at 07:27
  • 1
    Just a note: Internally, the layout guides are UIViews which conform to UILayoutSupport added to the view hierarchy. Maybe it could work if returning custom UIView subclasses after adding them to the view? – Accatyyc Jun 02 '14 at 12:21