我在iOS Swift上使用了PopOverPresentation控制器,并且在popover中同时放置了图像和文本?
我的问题是内容太大了,不适合popover?是否可以设置尺寸?
下面是我遇到的问题以及试用代码和错误消息的图片?
let myAlert = UIAlertController(title: "\n\n\n\n\n\n", message: "Correct answer selected, move on to next level", preferredStyle: UIAlertControllerStyle.actionSheet)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default)
{
// action in self.dismiss(animated: true, completion: nil)
action in self.performSegue(withIdentifier: "goSegue1", sender: self)
self.musicEffect.stop()
}
// put image into action sheet
let imageView = UIImageView(frame: CGRect(x: 70, y: -30, width: 140, height: 140))
imageView.image = #imageLiteral(resourceName: "wellDone2.png")
myAlert.addAction(okAction);
myAlert.view.addSubview(imageView)
// display the alert messgae
myAlert.popoverPresentationController?.sourceView = view
myAlert.popoverPresentationController?.sourceRect = (sender as AnyObject).frame
myAlert.preferredContentSize = CGSize(width: 500, height: 800)
Issue
最佳答案
弹出窗口的大小不受PopoverPresentationController
控制。PopoverPresentationController
只指定如何显示内容,但内容本身(包括大小)由从中获取控制器的视图设置,在您的情况下为myAlert
。
您尝试设置preferredContentSize
的想法是正确的,但设置的视图是错误的。你应该换线
popoverPresentationController?.preferredContentSize = /* ... */
致:
myAlert.preferredContentSize = /* ... */
这应该能解决问题。