如何将自定义大小设置为模态呈现 ViewController

How to set Custom size to Present Modally ViewController

我有 ViewController,当用户点击它的底部时,其他 ViewController 使用 segue Present Modally 弹出。 是否可以为他们提供自定义尺寸?我尝试通过 Use Preferred Explicit Size 选项提供内容大小,但没有帮助。我希望 VC 弹出并从上方获取当前 VC 的 70%。

查看此内容,如果您需要任何说明,请告诉我。

__weak IBOutlet UIView *conViewers;

- (void)callingOfCustomView:(UIViewController *)showPushedVC
{

    [showPushedVC.view setTranslatesAutoresizingMaskIntoConstraints:YES];
    showPushedVC.view.frame = conViewers.bounds;
    //**>> Add child view into container view
    [conViewers addSubview: showPushedVC.view];
    [self addChildViewController: showPushedVC];
    [showPushedVC didMoveToParentViewController:self];
}

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

    ViewersVC *objViewers = [storyboard instantiateViewControllerWithIdentifier:@"ViewersVC"];

    [self callingOfCustomView:objViewers];

将在 swift 中更新。

你应该在你的第二个 VC 上添加一个 70% 大小的视图(我的意思是你正在展示的),并将 VC's view 的背景颜色设置为 clear color 所以因为剩下的 30% 将显示以前的 VC。

例如你的 VC 的高度是 100 像素然后添加另一个 view 高度为 70 pixel 并将 clear background color 设置为 self.view 我的意思是VC!

的主视图

现在将 modalPresentationStyle 设置为您的 VC,然后再展示它,

  yourVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;

     [self presentViewController:yourVC animated:YES completion:^{


    }];

如果您提供的支持少于 ios 8,那么您需要将 setModalPresentationStyle 设置为 NavigationController(If you are using else parent view controller - which are presenting view controller),例如,

     [self.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];

     [self presentViewController:yourVC animated:YES completion:^{


    }];

Swift :

    yourVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

    self.presentViewController(yourVC, animated: true) {

    }

    self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext

    self.presentViewController(yourVC, animated: true) {

    }

使用 UIContainerView 而不是 "Present Modally" segue 解决了这个问题。

我就是这样做的,以防其他人遇到同样的问题。

不要将任何 SEGUE/EMBED 设置为 VC B/W UIContainerView。添加故事板 ID。

@IBOutlet weak var containerView: UIView!
weak var currentViewController: UIViewController?

override func viewDidLoad() {

    self.currentViewController = self.storyboard?.instantiateViewController(withIdentifier: "ContainerVC")
    self.currentViewController!.view.translatesAutoresizingMaskIntoConstraints = false
    self.addChildViewController(self.currentViewController!)
    self.addSubview(subView: self.currentViewController!.view, toView: self.containerView)

    super.viewDidLoad()

}
//MARK:- CONTAINER VIEW

func addSubview(subView:UIView, toView parentView:UIView) {
    parentView.addSubview(subView)

    var viewBindingsDict = [String: AnyObject]()
    viewBindingsDict["subView"] = subView
    parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[subView]|",
                                                                             options: [], metrics: nil, views: viewBindingsDict))
    parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[subView]|",
                                                                             options: [], metrics: nil, views: viewBindingsDict))
}

func cycleFromViewController(oldViewController: UIViewController, toViewController newViewController: UIViewController) {
    oldViewController.willMove(toParentViewController: nil)
    self.addChildViewController(newViewController)
    self.addSubview(subView: newViewController.view, toView:self.containerView!)
    newViewController.view.alpha = 0
    newViewController.view.layoutIfNeeded()
    UIView.animate(withDuration: 0.5, animations: {
        newViewController.view.alpha = 1
        oldViewController.view.alpha = 0
    },
                               completion: { finished in
                                oldViewController.view.removeFromSuperview()
                                oldViewController.removeFromParentViewController()
                                newViewController.didMove(toParentViewController: self)
    })
}

当您想根据操作更改视图时

    let newViewController : DestinationVC = self.storyboard?.instantiateViewController(withIdentifier: "DestinationVC") as! DestinationVC
    newViewController.data = dataToBeSentViaSegue // Passing DATA
    newViewController.view.translatesAutoresizingMaskIntoConstraints = false
    self.cycleFromViewController(oldViewController: self.currentViewController!, toViewController: newViewController)
    self.currentViewController = newViewController