gpt4 book ai didi

iphone - 如何将 Storyboard中创建的 UIView 添加为 Swift 中的弹出窗口?

转载 作者:行者123 更新时间:2023-11-28 08:34:26 24 4
gpt4 key购买 nike

我做了以下事情:1.拖拽UIView放置在ViewController功能区 Dragged UIView to place it in the ViewController ribbon

  1. 创建了一个自定义 UIView 类并将导出添加到“编辑配置文件 View ” View 字段:

导入 UIKit

类 EditProfileView:UIView {

let globalDataHandler = GlobalDataHandler.sharedInstance

@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var lastNameTextField: UITextField!
@IBOutlet weak var userNameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBAction func saveEditButton(sender: AnyObject) {
}
@IBAction func cancelEditButton(sender: AnyObject) {
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/

  1. 将“EditProfileView”类作为Storyboard 中“Edit Profile View” View 的自定义类。

  2. ProfileViewController 中为“EditProfileView”类创建了一个对象,并将“Edit Profile View” View 添加到主视图点击 ProfileViewController 中的编辑按钮。

类 ProfileViewController: UIViewController {

let profileView = EditProfileView()

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func editProfileButton(sender: AnyObject) {
profileView.firstNameTextField.text = "First Name"
profileView.lastNameTextField.text = "Last Name"
profileView.userNameTextField.text = "User Name"
profileView.emailTextField.text = "Email"
let testFrame : CGRect = CGRectMake(50,100,300,300)
profileView.frame = testFrame
self.view.addSubview(profileView)
}

但是,“编辑配置文件 View ” View 不会出现在 ProfileViewController 上。请帮忙。

最佳答案

您必须在 Xib 中链接您的 UIView 和您的 View (应该是一个不同的 UIViewController)。

profileView = storyboard.instantiateViewControllerWithIdentifier("ProfileViewController")

您的 ProfileVC 必须有一个 UIPresentationController

class ProfilePresentationController : UIPresentationController {
override func frameOfPresentedViewInContainerView() -> CGRect {
return CGRect(x: (presentingViewController.view.frame.width / 2.0) - 150.0, y: (presentingViewController.view.frame.height / 2.0) - 100.0, width: 300.0, height: 200.0)
}
}

您的 EditProfileView 必须实现以下委托(delegate):

UIViewControllerTransitioningDelegate

您的 profileView 的以下属性也必须设置为:

profileView.modalPresentationStyle = UIModalPresentationStyle.Custom
profileView.transitioningDelegate = self

之后你可以实现委托(delegate)所需的方法

func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
return ProfilePresentationController(presentedViewController: presented, presentingViewController: presenting)
}

关于iphone - 如何将 Storyboard中创建的 UIView 添加为 Swift 中的弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38207929/

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