2016-12-29 82 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

相關問題