io8 如何设置 UIPopoverPresentationController 大小

io8 How to set UIPopoverPresentationController size

我设置了 UIPopoverPresentationController 来显示 UIViewController 和 UIAlertView 显示。

但是当我创建自定义的 UIViewController 并使用 UIPopoverPresentationController 时。有显示全屏。

我想建立这样的效果 http://cocoa.tumblr.com/post/92070238973/how-can-i-use-both-uipopoverpresentationcontroller

我曾尝试设置 preferredContentSize,但它仍然显示全屏。

我的代码如下:

 - (void)viewDidLoad {
     [super viewDidLoad];
     contentVC = [UIViewController new];
     contentVC.view.backgroundColor = [UIColor darkGrayColor];
     contentVC.preferredContentSize = CGSizeMake(200, 200);


     UIPopoverPresentationController *popPC =      contentVC.popoverPresentationController;
     popPC.delegate = self;
     popPC.sourceView = self.view;
     popPC.sourceRect = CGRectMake(0,0,0,0);
     popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
 }

 - (IBAction)btnAction:(id)sender {

 [self presentViewController:contentVC animated:NO completion:ni

 }

 -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
 {
     return UIModalPresentationOverCurrentContext;
 }

有没有人知道如何在uiviewcontroller和UIPopoverPresentationController中设置size像网页效果?

我将在 UIViewController 中设置其他组件(现在只设置背景颜色)。

非常感谢。

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    return UIModalPresentationNone;
}

在您的代码下进行设置。

在情节提要中 select 导航控制器并设置内容大小值。

这也可以通过子类化 UINavigationController 并设置 preferredContentSize 在代码中完成。

- (void)viewDidLoad {
   [super viewDidLoad];
   self.preferredContentSize = CGSizeMake(100, 100);
}

Swift 3

我做了一个我想用这种方式使用的数字键盘,所以在数字键盘上viewController,你需要设置这些值

override func viewDidLoad()
{
    super.viewDidLoad()
    self.preferredContentSize = CGSize(width:340, height:385) // IMPORTANT

    // REST ARE COSMETIC CHANGES
    self.view.backgroundColor = UIColor.clear
    self.view.isOpaque = true        
    self.view.layer.masksToBounds = false
    self.view.layer.shadowOffset = CGSize(width: 0, height: 5)
    self.view.layer.shadowColor = UIColor(red:0, green:0, blue:0, alpha:1).cgColor
    self.view.layer.shadowOpacity = 0.5
    self.view.layer.shadowRadius = 20
}

我在 viewController 上展示它:

func showNumpad(view: UIView)
    {
        let controller: UIViewController? = numberpadVC()
        // present the controller
        // on iPad, this will be a Popover
        // on iPhone, this will be an action sheet
        controller?.modalPresentationStyle = .popover
        self.present(controller!, animated: true, completion: { _ in })
        // configure the Popover presentation controller
        let popController: UIPopoverPresentationController? = controller?.popoverPresentationController
        popController?.permittedArrowDirections = .any
        //popController?.barButtonItem = textField.inputView
        popController?.sourceView = view
        popController?.delegate = self
        popController?.sourceRect = (controller?.view.frame)! //CGRect(x: 0, y:0, width:340, height:384)
    }

正在显示数字键盘:

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool
{
    //self.scrollview.addSubview(self.numpad)
    UIView.animate(withDuration: 0.25)
    {
        //self.numpad.frame.origin.x = self.viewNum1.frame.origin.x - 330
        if textField == self.txtNum1
        {
            self.showNumpad(view: textField)
        }
    }
}

它看起来像这样: