如何仅在右侧显示自定义视图?

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

我想将我的观点显示为屏幕右侧的一列。此应用仅 iPad。

一种方法是创建一个视图控制器;从资产库中拖出一个新的视图控制器到你的故事板中,将背景颜色设置为黑色并将不透明度设置为 50%。然后拖出一个 UIView 并设置其约束,使其固定在右侧。您需要以模态方式呈现它。这通常是通过从原始视图控制器中的某个按钮或其他控件拖动到新视图控制器并选择 "present modally".

来完成的。

要重新创建您发布的图片中的效果,您不希望呈现视图控制器在后台消失。为了确保呈现视图控制器保持不变,您可以使用自定义呈现控制器。为此,将其添加到您的新视图控制器(在右侧显示视图的控制器):

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

现在创建一个 PresentationController,它只是 UIPresentationController 的子类。您需要在此 PresentationController 中实现的唯一方法是 shouldRemovePresentersView():

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

向呈现的(或第二个)视图控制器(包含 modalPresentationStyle = UIModalPresentationStyle.CustomtransitioningDelegate = self 的视图控制器)添加扩展或以其他方式符合 UIViewControllerTransitioningDelegate 协议:

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)
    }

}

您还可以使用完全可自定义的 CocoaControl:

REFrostedViewController

以编程方式创建控制器:

// 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;

或者只是子类化您的情节提要控制器。

希望对您有所帮助。

您应该使用容器视图。设置容器视图的宽度与主视图的宽度成比例。

现在你可以随意设计view controller(上图中的蓝色),它会按照你想要的方式显示。

然后 运行 在 iPad 模拟器中。您将在屏幕右侧查看。

希望对您有所帮助。