• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOS:学习笔记,用代码驱动自动布局实例(swift)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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(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人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap