(十二)swift UIButton 九宫格

(十二)swift UIButton 九宫格,第1张

概述1. 固定4个数目 代码如下: import UIKit import Cartography class WorkViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() createButton_Salary() createButton_Notic 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.}

}

总结

以上是内存溢出为你收集整理的(十二)swift UIButton 九宫格全部内容,希望文章能够帮你解决(十二)swift UIButton 九宫格所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1081585.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存