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 ...
随机推荐
- Spring实例化bean的三种方法
1.用构造器来实例化 <bean id="hello2" class="com.hsit.hello.impl.ENhello" /> 2.使用静态 ...
- 利用setTimeOut 和clearTimeOut 方法控制写一个 滑动导航显示不同信息的效果
效果如图鼠标滑动导航 下边显示不同效果 html代码和css格式代码 <body><div id="tab" class="tab"> ...
- java之Comparator与Comparable
转自:http://blog.csdn.net/zhangerqing 当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定 ...
- PAT---1001. A+B Format (20)
#include<stdio.h> int main(){ //定义要读入的两个整数 int a,b; //定义好放拆项用的数组 ]; //读入两个整数 scanf("%d%d& ...
- JavaScript读取txt文本文件方法详解
http://blog.163.com/sophie8910@126/blog/static/8304612620122834121264/ 第一步:创建一个可以将文件翻译成文件流的对象. Var f ...
- 节点类(CCNode)
节点与渲染树 回顾前面的介绍,我们已经知道了精灵.层和场景如何构成一个游戏的框架.精灵属于层,层属于场景,玩家与精灵互动,并导致游戏画面在不同场景中切换.把每个环节拼接在一起,我们得到了一个完整的关系 ...
- 基于Lucene的文件检索Demo
通过Lucene实现了简单的文件检索功能的Demo.这个Demo支持基于文件内容的检索,支持中文分词和高亮显示. 下面简单的介绍下核心的类 1)索引相关的类 1.FileIndexBuilder -- ...
- RedHat7 SELinux
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.NSA是在Linux社区的帮助下开发了一种 ...
- MKServerBuilder.psd1
MKServerBuilder.psd1 # # Module manifest for module 'MKServerBuilder' # # Generated by: Edward Guan ...
- php5魔术函数、魔术常量
魔术函数 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__destruct ...