Howto Size the Modal View programmatically (SWIFT)

2019-09-11 06:55发布

I simply want to present a small option dialog over an existing main UIViewController/UIView , so that on an IPad I would see a small Dialog and in the Background I will see the Main View.

I managed to show a UIViewController/UIView in a modal view style as follow:

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.sourceRect = CGRectMake(60, 100, 500, 500)
        //_popoverPresentationController. .setPopoverContentSize(CGSizeMake(550, 600), animated: true)
        //_popoverPresentationController.sourceView.sizeToFit();

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

But I have still some issues: 1. Howto get rid of the arrow shown at the border. 2. Howto size this modal view. It is shown to small and I would like to fit it to the largest controls in the UIControllerView/UIView.

any help ?

2条回答
闹够了就滚
2楼-- · 2019-09-11 07:51

I needed something similar and ended up presenting a view controller as a modal with a transparent background over the current context. There I made a smaller opaque UIView with what I wanted.

查看更多
劫难
3楼-- · 2019-09-11 07:53
  1. Try changing _popoverPresentationController.permittedArrowDirections to empty option set
  2. Change controller.preferredContentSize to match your desired size
查看更多
登录 后发表回答