虽然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, width: 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.

Swift下使用Xib设计界面的更多相关文章

  1. Swift下自定义xib添加到Storyboard

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51657154 ...

  2. WPF下的视频录制界面设计

    原文:WPF下的视频录制界面设计 在去年12月份,我曾经写过三篇文章讨论C#下视频录制.播放界面的设计.这三篇文章是:利用C#画视频录制及播放的界面(一) 利用C#画视频录制及播放的界面(二)利用C# ...

  3. 在Eclipse中用SWT设计界面

    摘自http://www.tulaoshi.com/n/20160129/1488574.html 在Eclipse中用SWT设计界面 1. 为什么要使用SWT?  SWT是一个IBM开发的跨平台GU ...

  4. swift -- 如何在swift下,使用类似oc的pch文件功能

    以前在做oc下的项目的时候,pch文件是必创建的,因为实在是太方便了,只要在build setting里面把pch的路径换成绝对路径,那么剩下的,想干什么就在里面干什么,但是swift下,可以实现这种 ...

  5. Mono For Android如何在VS2012 中打开设计界面

      刚接触 Mono For Android 没几天,不知不觉把设计界面弄丢了.辛辛苦苦才把设计界面弄出来,如果你在 Layout 下打开 *.xaml 的文件打开的却是 xml 文档,那么你可以按照 ...

  6. 使用xib开发界面

    使用xib开发界面 2015-02-02 10:03 编辑: suiling 分类:iOS开发 来源:jymn_chen‘s blog   纯代码写界面有时候会降低开发效率,对于一些通用简单的界面,例 ...

  7. eclipseMARS2.0使用可视化设计界面——配置window builder

    1.查看eclipse版本 不同版本的eclipse对应不同的版本window builder,如果不知道版本的话可以通过,点击HELP——>about eclipse来查看自己eclipse的 ...

  8. 剑指offer系列——二维数组中,每行从左到右递增,每列从上到下递增,设计算法找其中的一个数

    题目:二维数组中,每行从左到右递增,每列从上到下递增,设计一个算法,找其中的一个数 分析: 二维数组这里把它看作一个矩形结构,如图所示: 1 2 8 2 4 9 12 4 7 10 13 6 8 11 ...

  9. BCG界面库下的Windows8 UI界面样式www.webui8.com

    BCG界面库下的Windows8 UI界面样式(Metro风格)控件主要有以下一些功能: 规则的大块磁贴 支持完整键盘导航 Tile组 标题(Caption) 标题按钮(Caption buttons ...

随机推荐

  1. [转帖]浪潮openstack的逆袭

    扬长补短 浪潮云平台的逆袭 http://blog.itpub.net/11310314/viewspace-2564083/ 浪潮并不是最早加入OpenStack的国内厂商,却实现同比303.1%的 ...

  2. [转载]linux段页式内存管理技术

    原始博客地址: http://blog.csdn.net/qq_26626709/article/details/52742470 一.概述 1.虚拟地址空间 内存是通过指针寻址的,因而CPU的字长决 ...

  3. python逻辑回归 自动建模

    #-*- coding: utf-8 -*- #逻辑回归 自动建模 import numpy as np import pandas as pd from sklearn.linear_model i ...

  4. k8s 1.9 安装

    测试环境 主机 系统 master CentOS 7.3 node CentOS 7.3 2.关闭selinux(所有节点都执行) [root@matser ~]# getenforce Disabl ...

  5. python之random函数

    # random各种使用方法 import random # 随机生成[0.1)的浮点数 print("random():", random.random()) # 随机生成100 ...

  6. python---集合及深浅copy

    一.集合 相信大家都在数学中接触过集合,在python中集合也是有这相同的性质,1.去重,集合会将内部元素自定去重 2.关系测试,类似数学中的交集并集子集一样 1.1如何创建集合 集合在python中 ...

  7. HDU-5373-水题-卡常数时间

    姿势就是力量啊! 第一次意识到long long 比 int要慢很多.当时想到了各种优化仍然TLE,最后也没A出来,就是用了long long #include <cstdio> #inc ...

  8. windows service 安装/卸载

    第一种方法: 前提: Service1 中的serviceProcessInstaller1设置 Account为localSystem 1. 开始 ->运行 ->cmd(管理员身份运行) ...

  9. HDU1069 最长上升子序列

    emm....矩形嵌套 还记得吗....就是它... 直接贴代码了.... import java.util.ArrayList; import java.util.Arrays; import ja ...

  10. MT【11】对数放缩题

    解答:C 评论:这里讲几个背景知识