iOS: 学习笔记, 用代码驱动自动布局实例(swift)
来源:互联网 发布:淘宝上次认证信息地址 编辑:程序博客网 时间:2024/06/09 22:23
iOS自动布局是设置iOS界面的利器.
3. 修改控制器类
4. 运行
本实例展示了如何使用自动布局语言设置水平布局, 垂直布局
1. 创建空白iOS项目(swift)2. 添加一个控制器类, 修改YYAppDelegate.swift文件
@UIApplicationMainclass 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 UIKitclass 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(100, 100, 20, 20); self.view.addSubview(self.viewMoveBlock) var commandPane = UIView(frame:CGRectMake(0, 0, 160, 40)) //as UIView self.view.addSubview(commandPane) let BUTTONSIZE:CGFloat = 40 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(0), metrics:nil, views:views)) self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[commandPane(50)]", options:NSLayoutFormatOptions(0), 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": 40] for (k,v) in commands { v.setTranslatesAutoresizingMaskIntoConstraints(false) commandPane.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[\(k)(SIZE)]", options:NSLayoutFormatOptions(0), metrics:metrics, views:commands)) } commandPane.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[btnLeft(SIZE)][btnRight(SIZE)][btnUp(SIZE)][btnDown(SIZE)]-(>=0)-[btnOut(SIZE)][btnIn(SIZE)]|", options:NSLayoutFormatOptions(0), metrics:metrics, views: commands)) } func moveLeft() { self.moveTo(-20, 0) } func moveRight() { self.moveTo(20, 0) } func moveUp() { self.moveTo(0, -20) } func moveDown() { self.moveTo(0, 20) } func moveOut() { var rect = self.viewMoveBlock.frame; rect.origin.x -= 20; rect.origin.y -= 20; rect.size.width += 40; rect.size.height += 40; 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 += 20; rect.origin.y += 20; rect.size.width -= 40; rect.size.height -= 40; 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. 运行
0 0
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
- iOS: 学习笔记, 用代码驱动自动布局实例
- IOS autolayout自动布局实例(swift)
- IOS 自动布局篇 swift
- [iOS]Swift自动布局框架
- iOS学习笔记36-Masonry自动布局
- iOS学习笔记36-Masonry自动布局
- iOS学习笔记 Masonry自动布局
- IOS布局笔记一(代码实现自动布局)
- swift学习代码笔记
- IOS学习 autolayout 自动布局 代码与界面设置截图
- IOS代码实现自动布局
- IOS学习 constraintsWithVisualFormat自动布局
- 自动布局笔记(学习)
- iOS Swift+Foundation学习笔记
- Swift iOS 10 学习笔记
- iOS 圆形布局代码笔记
- Swift 自动布局
- MAC 配置环境变量
- 程序员必须知道的10大基础实用算法及其讲解
- matlab中的应用技巧
- 微信公众帐号开发教程第5篇-各种消息的接收与响应
- MyEclipse中配置struts.xml自动提示
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
- android RadioButton实现选项卡的效果
- POJ 3278 Catch That Cow
- [leetcode] Jump Game II
- 今天在看PHP视频时,学到了PHP中位运算的使用方式
- Java中的其他类和对象
- 计应121--电子商务论文--李智飞--201236616127
- 股票的基础知识、除权与除息
- 音视频编解码知识