• Swift下使用Xib设计界面


    虽然Swift可以纯代码设计界面,不过不利用现有的可视化工具有时候有点效率低。下面是使用xib设计方法,部分代码来自网上。

    (1)新建View

    2、新建View class

    3、DemoView.swift中

    class DemoView: UIView {

    // MARK:- 创建视图
    class func newInstance() -> DemoView? {
    let nibView = Bundle.main.loadNibNamed("DemoView", owner: nil, options: nil);

    if let view = nibView?.first as? DemoView {
    return view
    }
    return nil
    }

     4、ViewController中

    class LoginViewController: UIViewController {

    override func viewDidLoad() {
    super.viewDidLoad()
    var myView = Bundle.main.loadNibNamed("DemoView", owner: nil, options: nil)?.first as? DemoView

    myView?.frame = CGRect(x: 0, y: 0, self.view.frame.width-50, height: self.view.frame.height-140)
    myView?.center = self.view.center

    if myView != nil {
    self.view.addSubview(myView!)
    }
    }

    5、调用ViewController

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window=UIWindow(frame: UIScreen.main.bounds)
    window?.backgroundColor=UIColor.white
    window?.makeKeyAndVisible()
    window?.rootViewController=LoginViewController();
    //window?.rootViewController=MainViewController()
    return true
    }

    6、注意xib要设置

    7、添加几个控件效果

    8、点击两个圆环形状

    9、Button上右键连线代码

     10、运行点击按钮看到控制台显示adfa.

  • 相关阅读:
    BZOJ 1492 [NOI2007]货币兑换Cash
    BZOJ 2285 [Sdoi2011]保密
    BZOJ 2962 序列操作
    BZOJ 3197 [Sdoi2013]assassin
    BZOJ 3131 [Sdoi2013]淘金
    BZOJ 3532 [Sdoi2014]Lis
    BZOJ 1923 [Sdoi2010]外星千足虫
    BZOJ 4855 [Jsoi2016]轻重路径
    BZOJ 4289 PA2012 Tax
    微信开发之 微信支付
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/8457408.html
Copyright © 2020-2023  润新知