如何在视图控制器中调出 xib 作为"pop up"?



我已经将一个不错的登录框设计为xcode中的XIB文件,当我应用程序上的"登录页面"上的用户点击"登录"时,我希望登录框以pop pop向上,让他们输入他们的信息。

目前我很难露面,因为这是我第一次与XIBS合作。任何帮助,将不胜感激。看来我的iBaction没有连接,因为我完成了第二堂课,从而断开了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")
}
}

接受答案的问题是,仅在照顾视图本身中,这还不足以使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

"实现容器视图控制器"

您的容器视图控制器必须将儿童视图控制器与自己关联,然后再将孩子的根视图添加到视图层次结构

在您的项目弹出控制器中是UiviewController的一个子类,其中有几种方法可以添加这样的视图:

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

您可以参考如何在Swift中创建弹出窗口的教程,或使用XIB和情节板添加子视图

这就是您实际想要的。

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

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

创建类似的自定义.xib及其类文件。(例如CustomCell.SwiftCustomCell.xib

  • 转到Project Navigator,然后在右键上单击"创建新类",并带有CustomCell的名称,并带有UITableViewCell

  • 的子类
  • 转到Project Navigator并使用同一类名称

  • 创建新的.xib文件

新文件..->用户界面 ->视图 ->另存为您的类名称" CustomCell" -> create

  • 现在,您将获得带有Uiview子类的.xib文件,但是我们需要UITableViewCell,所以我们该怎么办?我们删除该uiview文件,并添加UITableViewCell并给出类名称CustomCell和标识符。(命令 shift l)uitaiteView并拖动到xib窗口&根据您的项目要求设计单元格。

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

  • 我们将从捆绑包中获取单元格,并用UitaiteView注册。

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

  • 使用此
  • 配置并加载单元格

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

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

注意:重复上述步骤替换UITableViewCellUICollectionViewCell替换为CC_19,然后注册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实例文件以在主窗口上添加XIB文件:您的AppDelegate看起来像这样。

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

  • 并在某些iBaction上添加您的customxib,例如:

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

  • 删除customxib,例如

self.CustomXIB?.removeFromSuperview()

最新更新