iOS: 学习笔记, 用代码驱动自动布局实例(swift)
iOS自动布局是设置iOS界面的利器.
本实例展示了如何使用自动布局语言设置水平布局, 垂直布局
1. 创建空白iOS项目(swift)
2. 添加一个控制器类, 修改YYAppDelegate.swift文件
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible() self.window!.rootViewController = MainViewController(nibName: nil, bundle: nil) return true
}
3. 修改控制器类
//
// MainViewController.swift
// UIByCode3_AutoLayout
//
// Created by yao_yu on 14-6-17.
// Copyright (c) 2014 yao_yu. All rights reserved.
// import UIKit class MainViewController: UIViewController { var viewMoveBlock: UIView! = UIView() init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
} override func viewDidLoad() {
super.viewDidLoad() self.viewMoveBlock.backgroundColor = UIColor.blueColor()
self.viewMoveBlock.frame = CGRectMake(, , , );
self.view.addSubview(self.viewMoveBlock) var commandPane = UIView(frame:CGRectMake(, , , )) //as UIView
self.view.addSubview(commandPane) let BUTTONSIZE:CGFloat =
var commands: Dictionary<String, UIButton> = [:]
var action:String
for name in ["Left", "Right", "Up", "Down", "In", "Out"]
{
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.setTitle(name, forState: UIControlState.Normal)
button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.addTarget(self, action: Selector("move\(name)"), forControlEvents: UIControlEvents.TouchUpInside)
commands["btn\(name)"] = button
commandPane.addSubview(button)
} var views = ["commandPane": commandPane] commandPane.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("[commandPane(260)]", options:NSLayoutFormatOptions(), metrics:nil, views:views))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[commandPane(50)]", options:NSLayoutFormatOptions(), metrics:nil, views:views))
self.view.addConstraint(NSLayoutConstraint(item: commandPane, attribute:NSLayoutAttribute.CenterX ,relatedBy:NSLayoutRelation.Equal, toItem:self.view ,attribute:NSLayoutAttribute.CenterX, multiplier:1.0, constant:0.0)) let metrics = ["SIZE": ]
for (k,v) in commands {
v.setTranslatesAutoresizingMaskIntoConstraints(false)
commandPane.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[\(k)(SIZE)]", options:NSLayoutFormatOptions(), metrics:metrics, views:commands))
}
commandPane.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[btnLeft(SIZE)][btnRight(SIZE)][btnUp(SIZE)][btnDown(SIZE)]-(>=0)-[btnOut(SIZE)][btnIn(SIZE)]|", options:NSLayoutFormatOptions(), metrics:metrics, views: commands)) } func moveLeft()
{
self.moveTo(-, )
} func moveRight()
{
self.moveTo(, )
} func moveUp()
{
self.moveTo(, -)
} func moveDown()
{
self.moveTo(, )
} func moveOut()
{
var rect = self.viewMoveBlock.frame; rect.origin.x -= ;
rect.origin.y -= ;
rect.size.width += ;
rect.size.height += ; UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1.0) self.viewMoveBlock.frame = rect;
UIView.commitAnimations()
} func moveIn()
{
var rect = self.viewMoveBlock.frame; rect.origin.x += ;
rect.origin.y += ;
rect.size.width -= ;
rect.size.height -= ; UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1.0) self.viewMoveBlock.frame = rect;
UIView.commitAnimations()
} func moveTo(x: CGFloat, _ y: CGFloat)
{
var p = self.viewMoveBlock.center; p.x += x;
p.y += y; UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1.0) self.viewMoveBlock.center = p;
UIView.commitAnimations()
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} /*
// #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ }
4. 运行
iOS: 学习笔记, 用代码驱动自动布局实例(swift)的更多相关文章
- iOS: 学习笔记, 用代码驱动自动布局实例
iOS自动布局是设置iOS界面的利器. 本实例展示了如何使用自动布局语言设置水平布局, 垂直布局 1. 创建空白iOS项目 2. 添加一个控制器类, 修改YYAppDelegate.m文件 #impo ...
- input子系统学习笔记六 按键驱动实例分析下【转】
转自:http://blog.chinaunix.net/uid-20776117-id-3212095.html 本文接着input子系统学习笔记五 按键驱动实例分析上接续分析这个按键驱动实例! i ...
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- iOS学习笔记之Category
iOS学习笔记之Category 写在前面 Category是类别(也称为类目或范畴),使用Category,程序员可以为任何已有的类添加方法.使用类别可以对框架提供的类(无法获取源码,不能直接修改) ...
- iOS学习笔记之ARC内存管理
iOS学习笔记之ARC内存管理 写在前面 ARC(Automatic Reference Counting),自动引用计数,是iOS中采用的一种内存管理方式. 指针变量与对象所有权 指针变量暗含了对其 ...
- iOS学习笔记06—Category和Extension
iOS学习笔记06—Category和Extension 一.概述 类别是一种为现有的类添加新方法的方式. 利用Objective-C的动态运行时分配机制,Category提供了一种比继承(inher ...
- IOS学习笔记48--一些常见的IOS知识点+面试题
IOS学习笔记48--一些常见的IOS知识点+面试题 1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...
随机推荐
- linq 学习笔记(一)
First: 找到符合条件的第一记录,就返回了,不管后面还有多少数据. Single: 先将记录都梳理一次,再找到符合要求的唯一记录. Single():操作一个集合,同时强要求只有一个对象匹配, ...
- json中文编码问题
在和微信接口交互的过程中需要传参json而里面有中文的存在,所以json_encode($data)编码后会形成 {"group":{"name":" ...
- PHP如何让apache支持.htaccess 解决Internal Server Error The server …错误
TP框架 打开 www.newtp.com/index.php/Home/Index/abc出现 如下错误: Internal Server Error The server encountered ...
- JavaScript- 省市联动代码
以下是JS省市联动菜单代码: 代码一: <html> <head> <title></title> <script language=" ...
- 理解position 绝对定位和相对定位
一.position的三种取值 1.取值 Position : static / absolute / fixed / relative static:静态 absolute:绝对定位 ...
- myecipse的debug调试操作方法
在myecipse如果想要查询某个变量的值,或者跟踪程序的执行流程,可以如下操作: 首先在程序中设置好断点(断点的设置方法,就是在想要设置的地方的行首双击,当一个蓝色的圆形实心图标显示出来,就证明你设 ...
- js验证邮箱
<html> <head> <script> function verifyAddress(obj) { var email = obj ...
- hdu2002
import java.util.*;class Main{public static void main(String args[]){Scanner cin=new Scanner(System. ...
- Ⅶ.AngularJS的点点滴滴-- 事件
事件(和js一样有冒泡和捕获) <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2 ...
- Android - Binder驱动
以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删): http://blog.csdn.net/luosh ...