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 ...
随机推荐
- C语言、Java的编译系统
Java是跨平台的. Java代码的编译是由Java语言的编译器来执行的,执行后生成的是.class文件,该文件是字节码文件. 然后交给虚拟机进行运行,虚拟机是在各个平台上可移植的.从而决定了Java ...
- webbrowser selstart selLength
附件:http://files.cnblogs.com/xe2011/Webbrowser_SelStart.rar 1 获得webBrowser光标所在的位置 2 设置webBrowser光标的位置 ...
- 百度的android采访分析
今天早上10分,我接到一个电话采访百度.当然,我说提前.我的病是多么强烈延迟.所以我也没怎么准备,当然,我也看他们的真实水平的思考.在这次审查中! ! ! ! ! !! .! ! ! !.!! !.! ...
- 使用java进行文件编码转换
在开发过程中,可能会遇到文件编码的转换,尽管说开发工具eclipse能够转换编码,可是有的情况却非常不方便.比方,原来文件本身的编码是GBK,如今要转换成UTF-8,假设直接在eclipse中把文件编 ...
- 《细说 new与 malloc 的 10 点区别》
http://www.jobbole.com/members/melonstreet/
- linux 提高进程优先级nice+ 进程调度CFS
http://www.cnblogs.com/wang_yb/archive/2012/09/04/2670564.htmlhttp://liwei.life/2016/04/07/linux%E7% ...
- windows修改mysql默认字符集不成功
今天下午弄了半天,终于把mysql的默认字符集弄成了, 按照网上的说法,什么修改mysql下面的my.ini文件,把参数设置成utf-8,我反复操作,结果都是不成功,后来我把mysql5.0卸载了,网 ...
- Quartz Enterprise Job Scheduler 1.x Tutorial---转载
Lesson 10: Configuration, Resource Usage and SchedulerFactory http://www.quartz-scheduler.org/docume ...
- Notification与多线程
来源:南峰子(@南峰子_老驴 ) 链接:http://t.cn/R5swQcR 前几天与同事讨论到Notification在多线程下的转发问题,所以就此整理一下. 先来看看官方的文档,是这样写的: I ...
- 获取IMEI码
核心代码: Imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId(); 1.加入权限 在manifes ...