SwiftUIButton使用教程

更新时间:2023-05-29 01:09:15 阅读: 评论:0

SwiftUIButton使⽤教程⼀.UIButton基本操作
1.创建按钮
let btn: UIButton = UIButton()//没有样式
let btns:UIButton =UIButton(type: UIButtonType)//有样式
let button = UIButton(frame:CGRect(x:10, y:150, width:100, height:30))//简化创建⽅式UIButtonType有以下类型
public enum UIButtonType : Int {
ca custom // no button type
@available(iOS 7.0, *)
ca system // standard system button
ca detailDisclosure
ca infoLight
ca infoDark
ca contactAdd
public static var roundedRect: UIButtonType { get } // Deprecated, u UIButtonTypeSystem instead
}
//使⽤
let btn: UIButton = UIButton(type: .Custom)
UIButton状态类型
/**
Normal (默认状态)
Highlighted (⾼亮状态)点击按钮不放
Disabled (使能状态)就是是否可⽤状态-->禁⽤的状态才会显现
Selected (选中状态)通过lected属性设置
*/google 英文
2、UIButton设置字内容和颜⾊
//显⽰⽂字
button1.tTitle("普通状态", for: .normal)
button1.tTitle("⾼粱状态", for: .highlighted)
button1.tTitle("禁⽤状态", for: .disabled)
//显⽰⽂字颜⾊
button1.d, for: .normal)
button1.tTitleColor(UIColor.blue, for: .highlighted)
button1.an, for: .lected)
button1.an, for: .disabled)
//阴影⽂字颜⾊设置
button1.an, for: .normal)
button1., for: .highlighted)
button1.tTitleShadowColor(UIColor.brown, for: .disabled)
button1.tTitleShadowColor(UIColor.darkGray, for: .lected)
3.UIButton设置背景颜⾊和背景图⽚
//背景颜⾊
button2.backgroundColor = ange
//背景图⽚
button4.tBackgroundImage(UIImage(named:"XXX"), for: .normal)
4.UIButton设置字体⼤⼩
button.titleLabel?.font = UIFont.systemFont(ofSize: 12)学日语哪里好
5.禁⽤UIButton
button.isEnabled = fal
button.isEnabled = true
6.设置圆⾓
Radius = 5
button.layer.masksToBounds = true
7.设置边框宽度/颜⾊
button.layer.borderWidth = 2
button.layer.borderColor = Color
8.设置背景图⽚为圆⾓
buttonImage.tImage(UIImage(named:"1") , forState: UIControlState.Normal)
//设置背景图⽚为圆⾓
buttonImage.imageView?.Radius = 50
默认情况下按钮会被渲染成单⼀颜⾊;系统蓝sprout
button.tImage(UIImage(named:"icon1"),forState:.Normal) //设置图标
button.adjustsImageWhenHighlighted=fal //使触摸模式下按钮也不会变暗(半透明)
discothequebutton.adjustsImageWhenDisabled=fal //使禁⽤模式下按钮也不会变暗(半透明)
也可以设置成保留图标原来的颜⾊
let iconImage = UIImage(named:"icon2")?.withRenderingMode(.alwaysOriginal)教师节动画
button.tImage(iconImage, for:.normal) //设置图标
button.adjustsImageWhenHighlighted = fal //使触摸模式下按钮也不会变暗(半透明)
button.adjustsImageWhenDisabled = fal //使禁⽤模式下按钮也不会变暗(半透明)
9.UIButton上图⽚和⽂字调整
UIButton上添加图⽚和⽂字,有时需要我们调整⽅向为逆时针⽅向,上、左、下、右依次去设置的btn.imageEdgeInts =UIEdgeIntsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat)
btn.titleEdgeInts =UIEdgeIntsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat)
实例如下:
//创建⼀个图⽚⼀个⽂字的按钮
let btn2: UIButton = UIButton(type: .Custom)
btn2.frame = CGRectMake(50, 100, 120, 35)
btn2.tImage(UIImage(named: "1"), forState: .Normal)
btn2.backgroundColor = UIColor.blackColor()
btn2.titleLabel?.font = UIFont.systemFontOfSize(20)
btn2.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
btn2.tTitle("图⽚按钮", forState: .Normal)
//偏移量,分别为上下左右
btn2.imageEdgeInts = UIEdgeIntsMake(0, -50, 0, 0)
btn2.titleEdgeInts = UIEdgeIntsMake(0, -80, 0, 5)
btn2.tTitleColor(UIColor.whiteColor(), forState: .Normal)
btn2.adjustsImageWhenHighlighted = fal
lf.view.addSubview(btn2)
10.添加按钮的点击事件azithromycin
按钮的触摸时间有以下类型
touchDown:单点触摸按下事件,点触屏幕
touchDownRepeat:多点触摸按下事件,点触计数⼤于1,按下第2、3或第4根⼿指的时候touchDragInside:触摸在控件内拖动时
touchDragOutside:触摸在控件外拖动时
touchDragEnter:触摸从控件之外拖动到内部时
touchDragExit:触摸从控件内部拖动到外部时
touchUpInside:在控件之内触摸并抬起事件
touchUpOutside:在控件之外触摸抬起事件
touchCancel:触摸取消事件,即⼀次触摸因为放上太多⼿指⽽被取消,或者电话打断button1.addTarget(lf,action:#lector(methodName), for: .touchUpInside)
button1.addTarget(lf, action:#lector(someMethod(button:)), for:.touchUpInside)
//上
func methodName() {financial crisis
print("tapped")
may it be}
//下
func someMethod(button:UIButton) {
print("你是谁啊,其实就是⼀个按钮")
}
⼆.⾃定义操作
1.UIButton的图⽚⽂字布局
创建⼀个按钮且其同时拥有⽂字和图⽚属性,会按照系统的默认样式(左图⽚,右⽂字)显⽰。但是有的时候,我们会遇到其他的设计需求,⽐如:左⽂字有图⽚、上⽂字下图⽚、上图⽚下⽂字等。这个时候就需要我们⾃定义按钮的显⽰样式来满⾜复杂多变的设计需求了。我⾃定义了⼀个按钮的extension来满⾜以上的功能,代码如下:
华尔街英语课程价格import Foundation
import UIKit
enum ButtonLayout {
ca leftImage
ca rightImage
ca topImage
ca bottomImage
}
extension UIButton {
func tLayoutType(type: ButtonLayout){
let image: UIImage? = lf.imageView?.image
switch type {
ca .leftImage:
print("系统默认的⽅式")
ca .rightImage:
lf.imageEdgeInts = UIEdgeInts(top:0, left: (lf.titleLabel?.frame.size.width)!, bottom: 0, right:-(lf.titleLabel?.frame.size.width)!)
lf.titleEdgeInts = UIEdgeInts(top: 0, left: -(image?.size.width)!, bottom: 0, right: (image?.size.width)!)
ca .topImage:
lf.imageEdgeInts = UIEdgeInts(top:-(lf.titleLabel?.frame.size.height)!, left: 0, bottom: 0, right:-((lf.titleLabel?.frame.size.width)!))
//图⽚距离右边框距离减少图⽚的宽度,距离上m边距的距离减少⽂字的⾼度
lf.titleEdgeInts = UIEdgeInts(top: ((image?.size.height)!), left: -((image?.size.width)!), bottom: 0, right:0)
东方阶梯双语学校//⽂字距离上边框的距离增加imageView的⾼度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变
default:
lf.imageEdgeInts = UIEdgeInts(top: (lf.titleLabel?.frame.size.height)!, left:0, bottom: 0, right:-((lf.titleLabel?.frame.size.width)!))
//图⽚距离上边距增加⽂字的⾼度距离右边距减少⽂字的宽度
lf.titleEdgeInts = UIEdgeInts(top: -(image?.size.height)!, left: -(image?.size.width)!, bottom: 0, right: 0)
}
}
}
以上就是Swift UIButton使⽤教程的详细内容,更多关于Swift UIButton的资料请关注其它相关⽂章!

本文发布于:2023-05-29 01:09:15,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/126393.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:按钮   触摸   设置   距离   事件
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图