Formsheet ios 8 constraints are same as iphones constraints

百般思念 提交于 2020-01-01 08:43:13

问题


I have this problem where formsheet in ios 8 is taking the constraints set for "compact - width regular -height" (that is all iPhones constraints) instead of "any- any" or "regular -width regular -height". I have two different design for iPhone and iPad since the formsheet is consuming iPhones constraint iam not able to achieve the same. Any help on this would be aprreciatd


回答1:


From the UIViewController class reference:

In a horizontally regular environment, a presentation style that displays the content centered in the screen. The width and height of the content area are smaller than the screen size and a dimming view is placed underneath the content. If the device is in a landscape orientation and the keyboard is visible, the position of the view is adjusted upward so that the view remains visible. All uncovered areas are dimmed to prevent the user from interacting with them.

In a horizontally compact environment, this option behaves the same as UIModalPresentationFullScreen.

Because the form sheet presentation on iPad is compact width and regular height, these are the values you'll get when you present a form sheet.


If you don't want the default size classes you can override them.

If your view controller is a child view controller of another, you can use setOverrideTraitCollection(_:forChildViewController:) and override the size class constraints for the child controller.

If your view controller is NOT a child view controller, you're not really supposed to change the trait collection, but you can do it using this hack.


The best solution would be to design your view controller to look appropriate in the default (correct) size constraints applied for a form sheet view controller presentation. You can usually do this by avoiding setting width constraints and only setting leading and trailing constraints.




回答2:


I found a different way to solve this for presented view controllers, such as form sheets which are not child view controllers.

I override viewWillLayoutSubviews and then layout based on the presenting view controller's trait collection.

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    if let presenting = presentingViewController {
        updateLayout(forSizeClass: presenting.traitCollection.horizontalSizeClass)
    }
}

Where updateLayout(forSizeClass:) is our function which does whatever we need to do to support the trait environment.

The benefit of this approach is that we are not explicitly checking the device type (.pad or .phone) and we are not explicitly thresholding the view size (which may change in the future). This approach natively supports iPad split view, allowing your layout to fallback to an iPhone style layout when the presenting view controller hits a .compact size class.

The primary assumption here is that your presenting view controller is full screen. But for form sheets this is often the case.




回答3:


Another solution is to use vc.presentationController.overrideTraitCollection

    // in landscape mode, we want the horizontal traits to be the same as the main screen
    if ( UIScreen.mainScreen.traitCollection.horizontalSizeClass ==  UIUserInterfaceSizeClassRegular){

        //if you use UIModalPresentationFormSheet, the traits horizontal will be compact , even on iPad, so we have tell the presentationcontroller to force the horizontaltraits to regular
        vc.modalPresentationStyle=UIModalPresentationFormSheet;
        vc.presentationController.overrideTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular ];
        [_rootViewController presentViewController:vc animated:true completion:^{}];
}


来源:https://stackoverflow.com/questions/27162615/formsheet-ios-8-constraints-are-same-as-iphones-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!