(十二)swift UIButton 九宫格
2015-12-15 10:37
393 查看
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. }
}
相关文章推荐
- (十一)swift 使用SQLite
- swift-tableView 代理方法实现点击按钮删除效果
- swift-tableView 删除行
- swift学习日记(六) 方法
- swift !?
- Swift的74标准功能
- Swift之协议( protocol)
- Swift之互斥锁的使用
- Swift学习笔记 -- 对象和类
- IOS_Swift_TYPEALIAS 和泛型接口
- Swift使用第三方库MJExtension
- 创建自注册的Swift UI 控件
- swift- 去除tableView 多余行
- Swift Swift中的反射
- swift-商品加入购物车动画
- Swift学习(持续更新)
- iOS8 用AVAudioPlayer播放音乐(Swift)
- 银行卡扫描(基于swift)
- 《从零开始学Swift》学习笔记(Day48)——类型检查与转换
- 《从零开始学Swift》学习笔记(Day48)——类型检查与转换