gpt4 book ai didi

ios - 更改 AlertController 的布局

转载 作者:行者123 更新时间:2023-11-29 02:10:40 24 4
gpt4 key购买 nike

我使用以下代码尝试使用 Nib 更改 UIAlertController 的布局,但是无论指定的 Nib 如何,对话框都会显示并且每次看起来都一样,它看起来像一个半透明的灰色框,在底部我的屏幕。

class AlertDialogViewController: UIViewController {

var message: String = ""

override init() {
super.init(nibName: "SignUpViewController", bundle: nil)
//Below two lines are important for custom transitions.
transitioningDelegate = self
modalPresentationStyle = .Custom
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

//Other code for your dialog controller
// .
// .
// .
}

extension AlertDialogViewController : UIViewControllerAnimatedTransitioning {

func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
return 0.5 //Add your own duration here
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
//Add presentation and dismiss animation transition here.
}
}

extension AlertDialogViewController : UIViewControllerTransitioningDelegate {

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}

}

extension UIViewController {

func showAleartWithMessage(message: String) {
var ad = AlertDialogViewController()
ad.message = message
presentViewController(ad, animated: true, completion: nil)
}
}

最佳答案

你不能

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

编辑:添加了我在评论中所说的相关代码

假设您想要一个带有一个 UILable 和两个 UIButtons 实例的对话框

class CustomView : UIView {
var commentLabel: UILable!
var okayButton: UIButton!
var cancelButton: UIButton!

init(frame: CGRect) {
super.init(frame: frame)

commentLabel = UILabel()
okayButton = UIButton.buttonWithType(.Custom)
cancelButton = UIButton.buttonWithType(.Custom)

// TODO: Configuration such target, action, titleLable, etc. left to the user

[commentLabel, okayButton, cancelButton].map { self.addSubview($0) }
}

@IBAction func okayButtonAction(sender: AnyObject) {
// TODO: Complete implementation
}

@IBAction func okayButtonAction(sender: AnyObject) {
// TODO: Complete implementation
}

}


class CustomAlertDialogViewCongroller : UIViewController {
override func loadView() {
self.view = CustomView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
}
}

// In the view controller that you want to present that alert dialog. Let's call it viewController

let customAlertDialogViewController = CustomAlertDialogViewCongroller()
customAlertDialogViewController.modalPresentationStyle = .UIModalPresentationFormSheet
customAlertDialogViewController.modalTransitionStyle = .CoverVertical

viewController.presentViewController(customAlertDialogViewController, animated: true, completion: nil)

关于ios - 更改 AlertController 的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29322939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com