2016-12-29 49 views
1

我有ViewController,当用户点击它的底部时,其他ViewController使用segue Present Modally弹出。 是否可以给他们自定义大小?我尝试通过Use Preferred Explicit Size选项给内容大小,但它没有帮助。我希望这个VC能够弹出并从当前的VC中拿走70%的VC。如何设置自定义大小以呈现Modally ViewController

+0

你在iPhone或iPad上尝试? –

+0

仅限iPhone。 – Nitesh

+0

你可以看看这个http://stackoverflow.com/questions/25811199/ios-8-change-the-size-of-presented-modal-view-controller –

回答

0

使用UIContainerView代替 “现模态” SEGUE解决它。

这是我如何做,以防其他人正在努力解决同样的问题。

请勿设置任何分段/嵌入到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) 
    }) 
} 

然后,每当你想改变VIEW据Action

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 
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中进行更新。

0

你应该在你的第二个VC(我的意思是,你正在演示),并设置背景是VC's viewclear color添加颜色的大小70%的视图,以便为剩余的30%将显示以前的VC。

例如你的VC的高度,然后100像素与70 pixel高度再添view,并设置clear background colorself.view我的意思是VC的主要观点!

现在设置modalPresentationStyle到您的VC提出它像以前一样,

yourVC.modalPresentationStyle = UIModalPresentationOverCurrentContext; 

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


    }]; 

如果你给的支持不是ios 8较小,那么你需要设置setModalPresentationStyleNavigationController(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) { 

    } 
+0

但我需要在按钮上执行的操作,这些操作将在视图的30%以下。并根据按钮的选择,我的上述内容发生变化。 – Nitesh

+0

也有这个错误,这个'原因:'应用程序试图呈现一个主动控制器模态' – Nitesh

+0

如果你想处理该部分的行动,那么你应该去'ContainerView',或者你可以在当前视图添加相同种类的用户界面任何人都可以感觉到它是以前的! – Lion

相关问题