如何在 ViewController 中将 xib 作为 "pop up" 调出?

How to bring up a xib as a "pop up" in a ViewController?

我在 Xcode 中设计了一个很好的登录框作为 xib 文件,当用户在我的应用程序的登录页面上点击 "Log In" 时,我希望弹出登录框并让他们输入他们的信息。

目前我很难让它出现,因为这是我第一次使用 xibs。任何帮助,将不胜感激。似乎我的 IBAction 没有连接,因为我做了第二个 class,它断开了登录按钮与 IBAction 的连接。不确定如何解决这个问题。

提前致谢!使用 Swift 3 和 Xcode 上的最新版本。

import UIKit

class AuthMainViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.isHidden = true
}
}

class loginClass: UIView {

class func instanceFromNib() -> UIView {
    return UINib(nibName: "LoginView.xib", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}

@IBAction func loginBtnTapped(_ sender: Any) {
    let loginView = loginClass.instanceFromNib()
    self.addSubview(loginView)
    print("Login got pressed")
}
}

在您的项目中,Pop-up 控制器是 UIViewController 的子类,您可以使用几种方法添加视图,如下所示:

self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController", bundle: nil)
self.view.addSubview(popViewController)

创建方法可参考教程Pop up window in Swift OR Adding a subview using a XIB and Storyboard

已接受答案的问题在于,in 仅处理视图本身,这不足以让 UIKit 完全正常运行。 为了拥有功能齐全的 UIViewController,你应该使用这个:

//adding UIViewController to another UIViewController:
addChildViewController(viewController)
view.addSubview(viewController.view)
viewController.view.frame = view.bounds
viewController.didMove(toParentViewController: self)

//removing UIViewController from another UIViewController:
viewController.willMove(toParentViewController: nil)
viewController.view.removeFromSuperview()
viewController.removeFromParentViewController()

如果没有其他方法,您可能会发现很多方面的故障,例如:

  • 外观方法
  • 旋转方式
  • 过渡方法
  • 响应者链
  • 导航控制器、标签栏控制器属性

参考: https://developer.apple.com/reference/uikit/uiviewcontroller

"Implementing a Container View Controller"

Your container view controller must associate a child view controller with itself before adding the child's root view to the view hierarchy

这就是您真正想要的。

主要我们可以使用自定义.xib加载自定义UITableViewCell, UICollectionViewCell和自定义PopUp

  1. 加载自定义 .xib 作为 UITableViewCell

像这样创建一对自定义 .xib 及其 class 文件。(例如 CustomCell.SwiftCustomCell.xib

  • 转到项目导航器并右键单击创建新的class,名称为CustomCell,子class为UITableViewCell

  • 转到项目导航器并使用相同的 class 名称创建新的 .xib 文件

新建文件.. -> 用户界面 -> 查看 -> 另存为你的 class 名称“CustomCell” -> 创建

  • 现在您将获得带有 UIView 子class 的 .xib 文件,但我们需要一个 UITableViewCell 那么我们将做什么?我们删除那个 UIView 文件并添加一个 UITableViewCell 并给 class 名称 CustomCell 和标识符。 (Command + Shift + L) UITableView 并拖放到 xib window & 根据您的项目要求设计单元格。

  • 现在我们将转到代码部分并在我们的 ViewContoller Class 文件中初始化 .xib 并将其加载到 UITableView.

  • 我们将从我们的包中获取单元格并使用 UITableView 注册单元格。

UITableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")

  • 用这个
  • 配置并加载cellForRowAt indexPath中的单元格

let cell : CustomCell = self.(YourTableView).dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell return cell

  1. 加载自定义 .xib 作为 UICollectionViewCell

注意:重复上述步骤将 UITableViewCell 替换为 UICollectionViewCell 并像这样注册 UICollectionViewCell

self.collectionView.register(UINib(nibName: "CollectoinCustCell", bundle: nil), forCellWithReuseIdentifier: "CollectoinCustCell")

  • 用这个
  • 配置并加载cellForRowAt indexPath中的单元格

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell : CollectoinCustCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectoinCustCell", forIndexPath: indexPath) as! CollectoinCustCell return cell }

  • 自定义视图作为弹出窗口(以上述问题的适当答案结束)

  • 创建一对 xib 文件,此时您不必从 .xib 文件中删除 UIView。

  • 从您的包中获取 xib,例如:

  • let CustomXIB = Bundle.main.loadNibNamed("CustomXIB", owner: nil, options: nil)?.first as? CustomXIB

  • viewWillAppear

  • 中定义你的xib文件的框架
  • CustomXIB?.frame = UIScreen.main.bounds

创建 appDelegate 实例文件以在 main window 上添加 xib 文件:您的 AppDelegate 将如下所示。

var AppInstance: AppDelegate! @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { AppInstance = self } }

  • 并在某些 IBAction 上添加您的自定义 XIB,例如:

AppInstance.window?.addSubview(self.CustomXIB!)

  • 一样删除 CustomXIB

self.CustomXIB?.removeFromSuperview()