How do I present a custom view only along the right? [closed]

最后都变了- 提交于 2019-12-13 22:07:40

问题


I want to present my view as a column on the right side of the screen. This app will be iPad only.


回答1:


One way to do this would be to create a view controller that way; drag out a new view controller from the asset library into your storyboard, set the background color to black and bring the opacity to 50%. Then drag out a UIView and set its constraints so that it is pinned to the right side. You need to present it modally. This is usually done by control-dragging from some button or other control in the original view controller to the new view controller and selecting "present modally".

To recreate the effect like in the picture you posted, you don't want the presenting view controller to disappear in the background. To make sure the presenting view controller stays around, you can use a custom presentation controller. To do this, add this to your new view controller (the one that displays the view on the right side):

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    modalPresentationStyle = UIModalPresentationStyle.Custom
    transitioningDelegate = self
}

Now create a PresentationController, which is just a subclass of UIPresentationController. The only method you need to implement in this PresentationController is shouldRemovePresentersView():

override func shouldRemovePresentersView() -> Bool {
    return false
}

Add an extension to the presented (or second) view controller (the one that contains modalPresentationStyle = UIModalPresentationStyle.Custom and transitioningDelegate = self) or conform to the UIViewControllerTransitioningDelegate protocol some other way:

extension YourViewController: UIViewControllerTransitioningDelegate {

    // Need this presentation controller so that view controller in background
    // isn't deallocated and therefore appears through the background.
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? {
        return YourPresentationControllersName(presentedViewController: presented, presentingViewController: presenting)
    }

}



回答2:


You can also use a fully-customizable CocoaControl:

REFrostedViewController

Create controllers programatically:

// Create content and menu controllers
//
DEMONavigationController *navigationController = [[DEMONavigationController alloc] initWithRootViewController:[[DEMOHomeViewController alloc] init]];
DEMOMenuViewController *menuController = [[DEMOMenuViewController alloc] initWithStyle:UITableViewStylePlain];

// Create frosted view controller
//
REFrostedViewController *frostedViewController = [[REFrostedViewController alloc] initWithContentViewController:navigationController menuViewController:menuController];
frostedViewController.direction = REFrostedViewControllerDirectionLeft;

// Make it a root controller
//
self.window.rootViewController = frostedViewController;

Or just subclass your storyboard controllers.

Hope it helps.




回答3:


You should use Container view. Set the width of the container view proportional to that of the main view.

Now you can design the view controller(blue colored in above image) as you want and it will be displayed as you wanted.

Then just run it in an iPad simulator. You will get you view in the right side of the screen.

Hope this helps you.



来源:https://stackoverflow.com/questions/31253357/how-do-i-present-a-custom-view-only-along-the-right

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