食草堂银府 精品故事阅读鉴赏

加入收藏

您所在的位置:首页 > 生活资讯

生活资讯

ios14卡贴弹窗代码(iOS14 实现卡贴弹窗效果)

分类: 生活资讯 编辑 : 〃xnm 发布 : 2025-08-04 06:14:24

iOS14 实现卡贴弹窗效果

在 iOS14 中,苹果新增了一种卡贴式的弹窗效果。在用户进行某些操作时,会弹出一个卡片弹窗,提示用户一些信息。这个效果十分好看且适用于各种场景。下面我们来学习一下如何实现这个效果。

使用 UIAlertController 实现卡贴弹窗效果

在 iOS 中,我们可以使用系统提供的 UIAlertController 来实现卡贴弹窗效果。UIAlertController 是一个 UIViewController 的子类,可以创建不同风格的弹窗,包括警告框、操作表、卡片式弹窗等。

下面是一段示例代码,使用 UIAlertController 实现一个卡片式弹窗:

ios14卡贴弹窗代码(iOS14 实现卡贴弹窗效果)

    let alertController = UIAlertController(title: \"这是标题\", message: \"这是消息内容\", preferredStyle: .alert)    let okAction = UIAlertAction(title: \"确定\", style: .default, handler: nil)    alertController.addAction(okAction)    alertController.addTextField { (textField) in        textField.placeholder = \"输入框\"    }    let cancelAction = UIAlertAction(title: \"取消\", style: .cancel, handler: nil)    alertController.addAction(cancelAction)    present(alertController, animated: true, completion: nil)

上面的代码会创建一个带有一个输入框和两个按钮(确定和取消)的卡片式弹窗。我们可以通过给 alertController 添加 UIAlertAction 来设置按钮。同时,也可以通过 addTextField 方法来添加输入框。

ios14卡贴弹窗代码(iOS14 实现卡贴弹窗效果)

自定义 UIView 实现卡贴弹窗效果

除了使用 UIAlertController 之外,我们还可以自定义 UIView 来实现卡贴弹窗效果。这种方法可以更加灵活地定制弹窗的样式。

下面是一段示例代码,使用自定义 UIView 实现一个卡片式弹窗:

    let alertView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))    alertView.backgroundColor = .white    alertView.layer.cornerRadius = 10    let titleLabel = UILabel(frame: CGRect(x: 0, y: 20, width: 300, height: 30))    titleLabel.text = \"这是标题\"    titleLabel.textAlignment = .center    alertView.addSubview(titleLabel)    let messageLabel = UILabel(frame: CGRect(x: 0, y: 60, width: 300, height: 50))    messageLabel.text = \"这是消息内容\"    messageLabel.textAlignment = .center    messageLabel.numberOfLines = 0    alertView.addSubview(messageLabel)    let okButton = UIButton(type: .system)    okButton.frame = CGRect(x: 0, y: 130, width: 150, height: 50)    okButton.setTitle(\"确定\", for: .normal)    okButton.addTarget(self, action: #selector(okButtonAction), for: .touchUpInside)    alertView.addSubview(okButton)    let cancelButton = UIButton(type: .system)    cancelButton.frame = CGRect(x: 150, y: 130, width: 150, height: 50)    cancelButton.setTitle(\"取消\", for: .normal)    cancelButton.addTarget(self, action: #selector(cancelButtonAction), for: .touchUpInside)    alertView.addSubview(cancelButton)    alertView.center = view.center    view.addSubview(alertView)

上面的代码会创建一个带有标题、消息内容和两个按钮(确定和取消)的卡片式弹窗。我们通过创建 UIView,并向其中添加子视图来实现自定义弹窗。其中,我们使用 UILabel、UIButton 等组件来构造弹窗的内容。同时,我们也可以通过设置 UIView 的背景颜色、圆角等属性,来定制弹窗的外观。

ios14卡贴弹窗代码(iOS14 实现卡贴弹窗效果)

总结

iOS14 中新增的卡贴弹窗是一种好看、实用的效果。我们可以使用系统提供的 UIAlertController 或自定义 UIView 来实现这个效果。无论使用哪种方法,我们都可以根据自己的需求来调整弹窗的样式。

希望本文能够帮助大家实现自己的卡贴弹窗效果!

下一篇:大秦帝师马化飞第75集(大秦帝师:马化飞,终老江湖) 下一篇 【方向键 ( → )下一篇】

上一篇:永恒兴龙集团招聘(永恒兴龙集团诚邀有志之士加入) 上一篇 【方向键 ( ← )上一篇】