如何添加在故事板中创建的UIView作为Swift中的弹出窗口?
问题描述:
我做了以下: 1.拖动的UIView以将其放置在带的ViewController 如何添加在故事板中创建的UIView作为Swift中的弹出窗口?
- 创建的自定义UIView类和添加的出口到“编辑纵断面图'视场:
送 'EditProfileView' 类作为自定义类为 '编辑纵断面图' 视图中故事板。
创建关于“EditProfileView”类的对象在ProfileViewController和在点击ProfileViewController在编辑按钮添加了“编辑纵断面图”视图到主视图。
进口的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
}
*/
}
类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)
}
}
但 '编辑个人资料查看' 视图不上ProfileViewController出现。 请帮忙。
答
你必须在Xib中链接你的UIView和你的View(应该是一个独特的UIViewController)。
profileView = storyboard.instantiateViewControllerWithIdentifier("ProfileViewController")
你必须有一个UIPresentationController您ProfileVC
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必须实现以下委托:
UIViewControllerTransitioningDelegate
您profileView的下列属性还必须设置为:
profileView.modalPresentationStyle = UIModalPresentationStyle.Custom
profileView.transitioningDelegate = self
之后,你可以实现委托所需的方法
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
return ProfilePresentationController(presentedViewController: presented, presentingViewController: presenting)
}
你为什么不只是让你'通过在文档大纲和控制选择“编辑个人资料视图”拖入故事板视图的profileView'和IBOutlet中拖入你的'ProfileViewController'?在像EditProfileView()这样的代码中初始化它将不会产生所需的效果,因为覆盖初始值设定项可以设置子视图。 – beyowulf