1.view添加背景图片。 实现平铺
// 通过使用layer.contents 添加平铺的背景
self.view.layer.contents = UIImage(named: "图片名称")?.cgImage
2.获取当前设备的屏幕大小
var screen = UIScreen.main.bounds
let width = screen.width
let height = screen.height
3.label组件
// 自动换行
Label.lineBreakMode = NSLineBreakMode.byWordWrapping
Label.numberOfLines = 0
// text的位置。 居左居中居右
Label.textAlignment = .left //center rigtht
// 设置字体大小
Label.font = UIFont.systemFont(ofSize: 11)
// 加粗
Label.font = UIFont.boldSystemFont(ofSize: 11)
// 设置字体自适应
Label.adjustsFontSizeToFitWidth=true
// 设置组件圆角 如果设置为圆形 需要width=height 然后 值为height/2
item.layer.cornerRadius = 10
4.设置组件部分为圆角(偷师的) 很好用!!
// 使用方法
Label.layer.mask = self.configRectCorner(view: zbStatusLabel, corner: [.topRight,.bottomRight], radii: CGSize(width: 10, height: 10))
/// 圆角设置
/// - Parameters:
/// - view: 需要设置的控件
/// - corner: 哪些圆角
/// - radii: 圆角半径
/// - Returns: layer图层
func configRectCorner(view: UIView, corner: UIRectCorner, radii: CGSize) -> CALayer {
let maskPath = UIBezierPath.init(roundedRect: view.bounds, byRoundingCorners: corner, cornerRadii: radii)
let maskLayer = CAShapeLayer.init()
maskLayer.frame = view.bounds
maskLayer.path = maskPath.cgPath
return maskLayer
}
5.随机数的创建
// 随机取到[0,2)
let num:Int = Int(arc4random() % 2)
6.关于颜色
// 使用rgba
item.backgroundColor = UIColor.init(red: 156/255, green: 156/255, blue: 251/255, alpha: 1)
// 直接颜色
item.backgroundColor = UIColor.blue
7.设置主界面
// 在AppDelegate 文件中
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let rootCtrl = xxxxxCtrl()
self.window?.rootViewController = rootCtrl
return true
}
8.关于scrollview
// 位置,大小
scrollView.frame = screenFrame
// 是否是翻页的形式
scrollView.isPagingEnabled = true
// scrollview 的容量大小
scrollView.contentSize = CGSize(width: width * 3, height: height)
9.组件添加事件 !! 好用
// 启用交互,然后添加事件
scoreView.isUserInteractionEnabled = true
let scoreDetail = UITapGestureRecognizer(target: self, action: #selector(self.xqtDetailView))
scoreView.addGestureRecognizer(scoreDetail)