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

时间:2016-12-29 10:15:21

标签: ios swift uistoryboardsegue

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

3 个答案:

答案 0 :(得分:2)

如果您需要任何澄清,请查看此内容并告诉我。

__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中更新此内容。

答案 1 :(得分:1)

您应该在第二个VC(我的意思是您要呈现的内容)中添加70%大小的视图,并将VC's view的背景颜色设置为clear color,以便为此剩下的30%将显示之前的VC。

例如,您的VC高度为100 px,然后添加高度为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:^{


    }];

斯威夫特:

    yourVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

    self.presentViewController(yourVC, animated: true) {

    }

    self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext

    self.presentViewController(yourVC, animated: true) {

    }

答案 2 :(得分:0)

使用UIContainerView代替"以模态呈现"解决它SEGUE。

我就是这样做的,以防其他人在同样的问题上挣扎。

不要将任何SEGI / EMBED设置为VC B / W UIContainerView。添加STORYBOARD 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