简体   繁体   中英

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 拖动UIView将其放在ViewController功能区中

  1. Created a custom UIView class and added the outlets to the ' Edit Profile View ' view fields:

import UIKit

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
}
*/

}

  1. Gave ' EditProfileView ' class as the Custom Class for the ' Edit Profile View ' view in Storyboard .

  2. 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 .

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. Please help.

You have to link your UIView and your View (should be a distinct UIViewController) in the Xib.

profileView = storyboard.instantiateViewControllerWithIdentifier("ProfileViewController")

You must have an UIPresentationController for your 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)
    }
}

Your EditProfileView must implement the following delegate:

UIViewControllerTransitioningDelegate

the following properties of your profileView must also be set to:

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM