简体   繁体   中英

Howto show an UIViewController over a UIViewcontroller as like a Popoverview

Difficult to explain for me :-)

I already have: A main UIViewController/UIView -> works A second UIViewController/UIView which I show after a button is pressed -> works, second UIViewController/UIView is shown on top of screen and has the full screensize, so I can't see the first UIViewController/UIView

Because I realized my secondView holds only some small components, I would like: to show the second UIViewController/UIView over the first as like a popoverview, So the second is sized minimal and the first is still shown in the back.

I thought I could do simply the following:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("Options") as! UIViewController

    vc.modalPresentationStyle = .Popover <<-- Make simply Modal ?

    self.presentViewController(vc, animated: true, completion: nil)

But still the second is shown on full screen. Any help ?

Update I just managed to do by combining stuff found in the internet without knowing what I exactly I am doing :-)

func showoptions(){

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Options") as! UIViewController

controller.modalPresentationStyle = UIModalPresentationStyle.Popover

let popoverPresentationController = controller.popoverPresentationController

// result is an optional (but should not be nil if modalPresentationStyle is popover)
if let _popoverPresentationController = popoverPresentationController {

    // set the view from which to pop up
    _popoverPresentationController.sourceView = self.view;
    _popoverPresentationController.sourceView.sizeToFit();

    // present (id iPhone it is a modal automatic full screen)
    self.presentViewController(controller, animated: true, completion: nil)
}

}

The only prolem I have now is, that the size of the modal view is too small, some compontents are not fully visible.

尝试更改controller.preferredContentSize以匹配您所需的大小

Have you tried this? iOS -- how do you control the size of a modal view controller?

Bullet points: 1. do not use UIViewcontroller. use just UIView. 2. the first UIViewController's view will be unloaded if u loading a modal view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM