故事板中的自定义 alertViewController
Custom alertViewController in storyboard
我想显示一个弹出窗口(我只想到 UIAlertController
),如下图所示:
What I want to achieve
我在互联网上发现了一些类似的问题,但它们是基于代码的,这样很难将内容对齐到我想要的位置并处理用户交互。
有没有办法让我在 Storyboard
中创建一个单独的“Something”,然后在这里弹出它?我想了一个完整的 ViewController
但它覆盖了整个屏幕。
非常感谢任何建议或解决方案。
ViewController
不会一直填满整个屏幕。这只是默认设置。
将您的子控制器的 .modalPresentationStyle
属性 设置为 .overCurrentContext
并以模态方式呈现。
现在,如果您查看 Controller 具有透明背景,它将像警报一样呈现。
你需要以模态方式呈现它,所以让 VC 给它标识符并在任何地方加载它
let vc = self.storyboard?.instantiateViewController(withIdentifier: "popupID") as! PopupViewController
vc.sendedStr = "someContent"
vc.myImage = UIImage(named:"flag.png")
vc.providesPresentationContextTransitionStyle = true;
vc.definesPresentationContext = true;
vc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.present(vc, animated:true, completion: nil)
//
class PopupViewController : UIViewController {
var sendedStr:String?
var myImage:UIImage?
}
更重要的是像这样在IB中设置视图的背景
首先在故事板中设计 viewController 并记住您的 ViewController 主视图背景颜色应该是清晰的或将其 Alpha 更改为 0.5
添加以下扩展
extension UIViewController {
open func presentPOPUP(_ viewControllerToPresent: UIViewController, animated flag: Bool, modalTransitionStyle:UIModalTransitionStyle = .coverVertical, completion: (() -> Swift.Void)? = nil) {
viewControllerToPresent.modalPresentationStyle = .overCurrentContext
viewControllerToPresent.modalTransitionStyle = modalTransitionStyle
self.present(viewControllerToPresent, animated: flag, completion: completion)
}
}
之后就可以像下面这样使用了
if let alerVC = self.myStoryBoard.instantiateViewController(withIdentifier: "AlertMessageVC") as? AlertMessageVC {
self.presentPOPUP(alerVC, animated: true, modalTransitionStyle: .crossDissolve, completion: nil)
}
您还可以将 modalTransitionStyle 更改为以下选项
.coverVertical
.flipHorizontal
.crossDissolve
.partialCurl
希望对您有所帮助。 :)
我想显示一个弹出窗口(我只想到 UIAlertController
),如下图所示:
What I want to achieve
我在互联网上发现了一些类似的问题,但它们是基于代码的,这样很难将内容对齐到我想要的位置并处理用户交互。
有没有办法让我在 Storyboard
中创建一个单独的“Something”,然后在这里弹出它?我想了一个完整的 ViewController
但它覆盖了整个屏幕。
非常感谢任何建议或解决方案。
ViewController
不会一直填满整个屏幕。这只是默认设置。
将您的子控制器的 .modalPresentationStyle
属性 设置为 .overCurrentContext
并以模态方式呈现。
现在,如果您查看 Controller 具有透明背景,它将像警报一样呈现。
你需要以模态方式呈现它,所以让 VC 给它标识符并在任何地方加载它
let vc = self.storyboard?.instantiateViewController(withIdentifier: "popupID") as! PopupViewController
vc.sendedStr = "someContent"
vc.myImage = UIImage(named:"flag.png")
vc.providesPresentationContextTransitionStyle = true;
vc.definesPresentationContext = true;
vc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.present(vc, animated:true, completion: nil)
//
class PopupViewController : UIViewController {
var sendedStr:String?
var myImage:UIImage?
}
更重要的是像这样在IB中设置视图的背景
首先在故事板中设计 viewController 并记住您的 ViewController 主视图背景颜色应该是清晰的或将其 Alpha 更改为 0.5
添加以下扩展
extension UIViewController {
open func presentPOPUP(_ viewControllerToPresent: UIViewController, animated flag: Bool, modalTransitionStyle:UIModalTransitionStyle = .coverVertical, completion: (() -> Swift.Void)? = nil) {
viewControllerToPresent.modalPresentationStyle = .overCurrentContext
viewControllerToPresent.modalTransitionStyle = modalTransitionStyle
self.present(viewControllerToPresent, animated: flag, completion: completion)
}
}
之后就可以像下面这样使用了
if let alerVC = self.myStoryBoard.instantiateViewController(withIdentifier: "AlertMessageVC") as? AlertMessageVC {
self.presentPOPUP(alerVC, animated: true, modalTransitionStyle: .crossDissolve, completion: nil)
}
您还可以将 modalTransitionStyle 更改为以下选项
.coverVertical
.flipHorizontal
.crossDissolve
.partialCurl
希望对您有所帮助。 :)