Autolayout with UIScroller doesn't work on 3.5" device

I'm having problems with an Auto Layout iOS7 app. It has a scroller and a UINavigationController. When I run it on a 3.5" device the top bar of the UINavigationController "steals" space and the bottom of my layout is rendered off the bottom of the device. It is not cropped by the height of navigation bar, I can show or hide that and the problem remains.

Here's the 4.0" version:

在这里输入图像描述

And here is the broken 3.5"

在这里输入图像描述

Here is the structure of my views:

在这里输入图像描述

The outermost view contains a TPKeyboardAvoidingScrollView, a handy UIScrollView subclass available from https://github.com/michaeltyson/TPKeyboardAvoiding that let you easily manage showing and hiding the keyboard. Inside that is the UIView is is the subview that the TPKeyboardAvoidingScrollView will scroll. And inside that I have a UITableView and a UIView that is fixed height, contains some UI widgets, and should remain glued to the bottom of the window.

The contraints are setup fairly simply, each of the outer views is pinned to the leading, trailing, top and bottom edges. The UITableView and "gray" view are not. The gray view has a fixed height of 194 and is pinned to the bottom with a constant of zero. And of course the UITableView does not have a fixed height but it is pinned to the bottom with a constant of 194. The are also pinned to the leading, trailing and top. Oh, I also did Editor>Resolves AutoLayout Issues> Add Missing Constraints.

I do the usual tricks inside my UIViewController, in ViewDidLoad I do:

    self.automaticallyAdjustsScrollViewInsets = NO;
    _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    _scrollerSubView.translatesAutoresizingMaskIntoConstraints = YES;

I also populate the UITableView and do the usual reusable cell coding.

I've been playing around with the AutoLayout constraints but aside from them looking correct it doesn't seem to do anything. And of course I have the usual "Unable to simultaneously satisfy constraints" error in the Console but frankly, that doesn't help me.

2014-04-22 17:31:34.856 Sample[3695:60b] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0xa35cc60 UIView:0x8f446e0.centerY == TPKeyboardAvoidingScrollView:0x8f44070.centerY>",
    "<NSLayoutConstraint:0xa35d3b0 V:|-(0)-[TPKeyboardAvoidingScrollView:0x8f44070]   (Names: '|':UIView:0xa35d270 )>",
    "<NSLayoutConstraint:0xa35d440 V:[TPKeyboardAvoidingScrollView:0x8f44070]-(0)-|   (Names: '|':UIView:0xa35d270 )>",
    "<NSAutoresizingMaskLayoutConstraint:0xa364a00 h=--& v=--& UIView:0x8f446e0.midY == + 284>",
    "<NSAutoresizingMaskLayoutConstraint:0xa365120 h=-&- v=-&- UIView:0xa35d270.height == UIViewControllerWrapperView:0xa35c550.height>",
    "<NSAutoresizingMaskLayoutConstraint:0xa365880 h=-&- v=-&- UIViewControllerWrapperView:0xa35c550.height == UINavigationTransitionView:0x8f3a7a0.height>",
    "<NSAutoresizingMaskLayoutConstraint:0xa366140 h=-&- v=-&- UINavigationTransitionView:0x8f3a7a0.height == UILayoutContainerView:0x8f353a0.height>",
    "<NSAutoresizingMaskLayoutConstraint:0xa366710 h=-&- v=-&- UILayoutContainerView:0x8f353a0.height == UIWindow:0xa12ff50.height>",
    "<NSAutoresizingMaskLayoutConstraint:0xa367100 h=--- v=--- V:[UIWindow:0xa12ff50(480)]>"
)

Will attempt to recover by breaking constraint

I'm at a bit of a loss now how to proceed. This isn't that complex a situation but it has baffled me. I've read countless SO questions and I think I've tried everything but obviously I haven't.

How can I get this fairly simple UI to work for both 3.5" and 4" iOS7 devices?


Got it!

Just move the 3 above lines from viewDidLoad to viewDidAppear and it works just perfectly.

I figured this from Rembrandt Q. Einstein's comment on chandan's answer in My UIScrollView doesn't work with auto-layout in ios6

Just to clarify the reason this works: Autolayout works its magic in -layoutSubviews which happens AFTER viewDidLoad but before ViewDidAppear -- Rembrandt Q. Einstein

链接地址: http://www.djcxy.com/p/91592.html

上一篇: 在这种情况下正确布局?

下一篇: 带有UIScroller的Autolayout在3.5英寸设备上不起作用