我希望创建一个从UIButton
弹出的小型弹出窗口,大小约为50x50px
。我看到过使用自适应转场的方法,但我的尺寸类已关闭,意味着我无法使用此功能!
除此之外,我还能用其他方式创建这个弹出窗口吗?我能否在IBACtion
中使用代码创建它?或者还有其他使用storyboards的方法吗?
Create an action for the UIButton
in your UIViewController
and inside present the ViewController
you want like a Popover and your UIViewController
has to implement the protocol UIPopoverPresentationControllerDelegate
, take a look in the following code :
@IBAction func showPopover(sender: AnyObject) {
var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdentifier") as! UIViewController
popoverContent.modalPresentationStyle = .Popover
var popover = popoverContent.popoverPresentationController
if let popover = popoverContent.popoverPresentationController {
let viewForSource = sender as! UIView
popover.sourceView = viewForSource
// the position of the popover where it's showed
popover.sourceRect = viewForSource.bounds
// the size you want to display
popoverContent.preferredContentSize = CGSizeMake(200,500)
popover.delegate = self
}
self.presentViewController(popoverContent, animated: true, completion: nil)
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
According to the book of @matt Programming iOS 8:
A popover presentation controller, in iOS 8, is a presentation controller (
UIPresentationController
), and presentation controllers are adaptive. This means that, by default, in a horizontally compact environment (i.e. on an iPhone), the.Popover
modal presentation style will be treated as.FullScreen
. What appears as a popover on the iPad will appear as a fullscreen presented view on the iPhone, completely replacing the interface.
To avoid this behavior in the iPhone you need to implement the delegate method adaptivePresentationStyleForPresentationController
inside your UIViewController
to display the Popover correctly.
The other way in my opinion is more easy to do, and is using Interface Builder, just arrange from the UIButton
to create a segue to the ViewController
you want and in the segue select the Popover
segue.
Swift 4 这里是完全可用的代码。因此,您将看到大小为250x250的弹出窗口:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// in case if you don't want to make it via IBAction
button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
}
@objc
private func tapped() {
guard let popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") else { return }
popVC.modalPresentationStyle = .popover
let popOverVC = popVC.popoverPresentationController
popOverVC?.delegate = self
popOverVC?.sourceView = self.button
popOverVC?.sourceRect = CGRect(x: self.button.bounds.midX, y: self.button.bounds.minY, width: 0, height: 0)
popVC.preferredContentSize = CGSize(width: 250, height: 250)
self.present(popVC, animated: true)
}
}
// This is we need to make it looks as a popup window on iPhone
extension ViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
在此您可以通过按钮点击展示一个弹出窗口。
func addCategory( _ sender : UIButton) {
var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSizeMake(50,50)
popover.delegate = self
popover.sourceView = sender
popover.sourceRect = sender.bounds
self.presentViewController(nav, animated: true, completion: nil)
}
Swift 4版本
大部分工作都从故事板中完成
我添加了一个ViewController
,转到其属性检查器并选中“使用首选显式大小”。然后,我将宽度和高度值更改为50。
完成这一步骤后,我通过Ctrl单击并从按钮拖动到我添加的ViewController
,选择“作为弹出窗口呈现”并将segue标识符命名为“pop”
转到我有按钮的ViewController
并添加以下代码:
class FirstViewController: UIViewController, UIPopoverPresentationControllerDelegate {
@IBOutlet weak var popoverButton: UIButton! // the button
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pop" {
let popoverViewController = segue.destination
popoverViewController.modalPresentationStyle = .popover
popoverViewController.presentationController?.delegate = self
popoverViewController.popoverPresentationController?.sourceView = popoverButton
popoverViewController.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: popoverButton.frame.size.width, height: popoverButton.frame.size.height)
}
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
func adaptivePresentationStyleForPresentationController(PC: UIPresentationController!) -> UIModalPresentationStyle { // This *forces* a popover to be displayed on the iPhone return .None }
。请注意,此函数将强制在iPhone上显示弹出框。 - user1076813