如何在旋转期间模仿iOS Mail应用程序的自动布局更改?

时间:2014-11-17 20:18:59

标签: ios autolayout

我想模仿iOS Mail应用程序在左窗格中使用tableview筛选邮件的行为,以及预览邮件以便于阅读的右窗格。

我注意到在横向模式下,存在左侧邮件项目的tableview。但是当用户切换到纵向模式时,左侧窗格会完全消失,右侧的阅读窗格会平滑扩展以占用额外的空间。

当用户恢复为横向模式时,左窗格会再次出现,一切都恢复正常。

我正在尝试使用以下代码执行相同操作:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

    UIToolbar *toolbar = self.toolbar;
    UITableView *tableView = self.tableView;
    UIWebView *webView = self.webView;

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        [self.toolbar setHidden:YES];
        [self.tableView setHidden:YES];

        [self.view removeConstraint:self.equalWidthsToolbarWebviewConstraint];

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[webView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]];
    } else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        [self.toolbar setHidden:NO];
        [self.tableView setHidden:NO];



        //TODO: Set up views to have equal widths
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[toolbar][webView(==toolbar)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(toolbar,webView)]];

    }
}

当我从横向放置iPad处于纵向模式时,左窗格视图消失,右窗格最终会展开。但是当我把它带回景观时,左右窗格不会回到原来的布局。

这是我在调试器中得到的:

2014-11-17 15:16:02.721 Citta Streams iOS[2366:17353693] 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:0x7c971df0 UITableView:0x7c1b8800.leading == UIToolbar:0x7c976f90.leading>",
    "<NSLayoutConstraint:0x7c971e50 H:|-(0)-[UITableView:0x7c1b8800]   (Names: '|':UIView:0x7c971780 )>",
    "<NSLayoutConstraint:0x7c971eb0 H:[UIWebView:0x7c9f91a0]-(0)-|   (Names: '|':UIView:0x7c971780 )>",
    "<NSLayoutConstraint:0x7caa58f0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7c971780(768)]>",
    "<NSLayoutConstraint:0x7c9e06b0 UIWebView:0x7c9f91a0.leading == UIView:0x7c971780.leadingMargin>",
    "<NSLayoutConstraint:0x7b6c41a0 H:[UIToolbar:0x7c976f90]-(0)-[UIWebView:0x7c9f91a0]>",
    "<NSLayoutConstraint:0x7b6c6150 UIWebView:0x7c9f91a0.width == UIToolbar:0x7c976f90.width>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7b6c41a0 H:[UIToolbar:0x7c976f90]-(0)-[UIWebView:0x7c9f91a0]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

1 个答案:

答案 0 :(得分:1)

不要浪费时间用标准的UI组件复制它 - 苹果已将此功能内置到UISplitViewController中:

https://developer.apple.com/library/ios/documentation/uikit/reference/uisplitviewcontroller_class/index.html