(十二)swift UIButton 九宫格

本文介绍了一个使用SwiftUI实现的按钮布局方案,通过自定义视图控制器来创建不同位置的按钮,包括居中、居左、居右等布局,并且演示了如何根据屏幕宽度自适应调整按钮数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, 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_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, 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_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, 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)
}


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, y, button_width, 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.
}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值