(十二)swift UIButton 九宫格

前端之家收集整理的这篇文章主要介绍了(十二)swift UIButton 九宫格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 固定4个数目

代码如下:

import UIKit
import Cartography

class WorkViewController: UIViewController {

override func viewDidLoad() {


    super.viewDidLoad()


    createButton_Salary()

    createButton_Notice();

    createButton_Contants();

    createButton_Calendar();



}


// 工资 (第一行 居中)
func createButton_Salary() {

    let width = self.view.frame.width;
    let button_width = width / 4 ;  // button 的宽度
    let _top = (self.navigationController?.navigationBar.frame.height)! + 20 ; // (+20 statusBar)

    // ------------- button 的横纵坐标
    let x =  (width / 2) - button_width / 2; // 横坐标
    let y:CGFloat = _top + (button_width / 4) ;

    let button = UIButton(type: UIButtonType.System)
    button.frame = CGRectMake(x,y,button_width,button_width)


    button.backgroundColor = UIColor.blackColor();
    button.setTitle("工资",forState: UIControlState.Normal)
    //button.addTarget(self,action: "buttonPressed:",forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button)
}

// 通知(第一行 居左)
func createButton_Notice() {

    let width = self.view.frame.width;
    let button_width = width / 4 ;  // button 的宽度
    let _top = (self.navigationController?.navigationBar.frame.height)! + 20 ; // (+20 statusBar);

    // ------------- button 的横纵坐标
    let x =  (width / 4) - (button_width / 2) -  (button_width / 4) ; // 横坐标
    let y:CGFloat = _top  + (button_width / 4) ;

    let button = UIButton(type: UIButtonType.System)
    button.frame = CGRectMake(x,button_width)

    button.backgroundColor = UIColor.blackColor();
    button.setTitle("通知",forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button)
}

// 通讯录(第一行 居右)
func createButton_Contants() {

    let width = self.view.frame.width;
    let button_width = width / 4 ;  // button 的宽度
    let _top = (self.navigationController?.navigationBar.frame.height)! + 20 ; // (+20 statusBar);

    // ------------- button 的横纵坐标
    let x =  (width / 2) + (button_width / 2) + (button_width / 4) ;
    let y:CGFloat = _top  + (button_width / 4) ;

    let button = UIButton(type: UIButtonType.System)
    button.frame = CGRectMake(x,button_width)

    button.backgroundColor = UIColor.blackColor();
    button.setTitle("通讯",forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button)
}

// 日程(第二行 居左)
func createButton_Calendar() {

    let width = self.view.frame.width;
    let button_width = width / 4 ;  // button 的宽度
    let _top = (self.navigationController?.navigationBar.frame.height)! + 20 ; // (+20 statusBar);

    // ------------- button 的横纵坐标
    let x =  (width / 4) - (button_width / 2) -  (button_width / 4) ; // 横坐标
    let y:CGFloat = _top  + button_width + (button_width / 2)  ;


    let button = UIButton(type: UIButtonType.System)
    button.frame = CGRectMake(x,button_width)

    button.backgroundColor = UIColor.blackColor();
    button.setTitle("日程",forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

2. 数目自定

代码如下:
import UIKit
import Cartography

class WorkViewController: UIViewController {

override func viewDidLoad() {


    super.viewDidLoad()


    CreateSudoku();

}


func CreateSudoku()
{

    let width = self.view.frame.width;  // 屏幕宽度
    let button_width = width / 4 ;      // button 的宽度
    let _top = (self.navigationController?.navigationBar.frame.height)! + 20 ; // (+20 statusBar)

    var x:CGFloat = 0;   // 横坐标
    var y:CGFloat = _top ;   // 纵坐标
    var row_index:Int = 0;   // 行号

   for var i = 1 ; i <= 24 ;i++
    {
        y = _top;

        if i % 3 == 0
        {
            row_index = (i / 3) - 1;
        }
        else
        {
            row_index = (i / 3)
        }


        // ------------- button 的横坐标
        // 居左
        if i % 3 == 1
        {
            x =  (width / 4) - (button_width / 2) -  (button_width / 4) ; // 横坐标
        }

        // 居中
        if i % 3 == 2
        {
            x =  (width / 2) - button_width / 2; // 横坐标
        }

        // 居右
        if i % 3 == 0
        {
            x =  (width / 2) + (button_width / 2) + (button_width / 4) ;

        }

        // ------------- button 的纵坐标,使用循环,避免CGFLOAT * Init 的报错
        if row_index > 0
        {
            for var j = 0 ; j < row_index ; j++
            {
                y += button_width ;
            }
        }

        // ---- 纵坐标 间距
        for var m = 0 ; m <= row_index ; m++
        {
            y += button_width / 4 ;
        }

        let button = UIButton(type: UIButtonType.System)
        button.frame = CGRectMake(x,button_width)


        button.backgroundColor = UIColor.blueColor();
        button.setTitle( String(i),forState: UIControlState.Normal)
        self.view.addSubview(button)


    }

}





override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

原文链接:https://www.f2er.com/swift/325134.html

猜你在找的Swift相关文章