[英]How to add a UIView which is created in storyboard as a pop-up in Swift?
I did the following: 1. Dragged UIView to place it in the ViewController ribbon 我执行了以下操作:1。拖动UIView将其放在ViewController功能区中
import UIKit 导入UIKit
class EditProfileView: UIView { class 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
}
*/
} }
Gave ' EditProfileView ' class as the Custom Class for the ' Edit Profile View ' view in Storyboard . 将“ EditProfileView ”类作为Storyboard中 “ 编辑配置文件视图 ”视图的自定义类。
Created an object for ' EditProfileView ' class in ProfileViewController and added the ' Edit Profile View ' view to the main view upon clicking edit button in ProfileViewController . 在ProfileViewController中为“ EditProfileView ”类创建了一个对象,并在单击ProfileViewController中的“编辑”按钮时将“ 编辑配置文件视图 ”视图添加到主视图中。
class ProfileViewController: UIViewController { class 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)
}
} }
But, the ' Edit Profile View ' view doesn't appear on ProfileViewController. 但是,“ 编辑配置文件视图 ”视图不会出现在ProfileViewController上。 Please help.
请帮忙。
You have to link your UIView and your View (should be a distinct UIViewController) in the Xib. 你必须在Xib中链接你的UIView和你的View(应该是一个独特的UIViewController)。
profileView = storyboard.instantiateViewControllerWithIdentifier("ProfileViewController")
You must have an UIPresentationController for your ProfileVC 您必须拥有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)
}
}
Your EditProfileView must implement the following delegate: 您的EditProfileView必须实现以下委托:
UIViewControllerTransitioningDelegate
the following properties of your profileView must also be set to: 您的profileView的以下属性也必须设置为:
profileView.modalPresentationStyle = UIModalPresentationStyle.Custom
profileView.transitioningDelegate = self
After that you can implement the delegate required method 之后,您可以实现委托所需的方法
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
return ProfilePresentationController(presentedViewController: presented, presentingViewController: presenting)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.