UIButton控件是应用界面中常用的一个控件,用法总结: 一、初始化 UIButton的初始化一般使用其类方法,+ (id)buttonWithType:(UIButtonType)buttonType
//按钮初始化类方法 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];//这里创建一个圆角矩形的按钮 //按钮初始化实例方法 UIButton *button1=[[UIButton alloc]initWithFrame:CGRectMake(50, 300, 200, 50)]; //能够定义的button类型有以下6 是否在dealloc中对UIButton对象进行release操作,取决于UIButton初始化的方式。 如果使用 [[UIButton alloc]init]的方式,则需要主动进行release释放操作。 设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用: [btn.titleLabel setTextColor:[UIColorblackColor]]; btn.titleLabel.textColor
UIButton 的全面解析 建议收藏,用到的时候来这里一查就都明白了 //初始化Button 不用alloca init 的方法 用便利构造器初始化 UIButton *button = [UIButton
#import <UIKit/UIKit.h> @interface UIButton (MutableTitle) /** * 根据添加的title 改变 button 的长度 * * @ NSString *)text textFont:(UIFont *)textFont forState:(UIControlState)UIControlState; @end #import "UIButton +MutableTitle.h" @implementation UIButton (MutableTitle) - (void)setMutableTitleWithString:(NSString maxSize lineBreakMode:NSLineBreakByCharWrapping]; } return textSize; } @end 使用时 导入头文件 #import "UIButton +MutableTitle.h" UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)]; button.center
textField.borderStyle = UITextBorderStyle.line // textField.clearButtonMode = .whileEditing let btnLeft:UIButton = UIButton(type:UIButtonType.custom) btnLeft.frame = CGRect(x:0,y:0,width:30,height:30) btnLeft.setBackgroundImage UIControlState()) textField.leftView = btnLeft textField.leftViewMode = .unlessEditing let btnRight:UIButton = UIButton(type:UIButtonType.custom) btnRight.frame = CGRect(x:0,y:0,width:30,height:30) textField.rightViewMode UITextField)-> Bool { textField.resignFirstResponder() return true } func touchupInsideBtnAction(_ btn:UIButton
设置无限次循环 [animationImageView startAnimating];//开始播放 //[animationImageView stopAnimating];//停止播放 UIButton 但需要实现一个有点击事件的视图的时候,而这个视图内部的布局有需要自定义,这个时候就可以自定义一个UIButton,然后通过addSubview的方式给自定义的Button添加个性化组件和布局。 之前的时候,遇到上面的需求,我都是子类化一个UIView,然后给该子类添加手势,其实只需要子类化一个UIButton即可,这样就不用自己添加手势了!
第一、UIButton的定义 UIButton *button=[[UIButton buttonWithType:(UIButtonType); 能够定义的button类型有以下6种, typedef
#import <UIKit/UIKit.h> typedef void(^ButtonBlock)(UIButton* btn); @interface UIButton (Block) /** UIControlEvents)controlEvents; @end //////////////////////////////////////////////////// #import "UIButton +Block.h" #import <objc/runtime.h> @implementation UIButton (Block) static char ActionTag; /** * = [UIColor redColor]; [self.view addSubview:testButton]; [testButton addAction:^(UIButton *btn) { NSLog(@"我被点名了"); }]; [testButton addAction:^(UIButton *btn) {
最近一个项目有获取手机短信跟邮箱验证码功能, 所以要加一个UIButton倒计时功能 例子代码如下: 1 //获取验证码按钮 2 - (IBAction)getButtonClick:(UIButton *)sender; 3 4 #pragma mark - 获取验证码 5 - (IBAction)getButtonClick:(UIButton *)sender 6 { 7 / mainColor countColor:countColor]; 12 } 13 14 #pragma mark - button倒计时 15 - (void)setTheCountdownButton:(UIButton
UIButton 风格 typedef NS_ENUM(NSInteger, UIButtonType) { UIButtonTypeCustom = 0, UIButtonTypeRoundedRect = UIButtonTypeSystem, // Deprecated, use UIButtonTypeSystem instead }; 简单操作 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.tag = 100; // 给 Button 加入标记 在不同的方法中使用同一个控件 从父类视图中获取button UIButton *btn = (UIButton *)[self.window viewWithTag:100]; ; // 让 Button 失效 ; } - (void) buttonAction:(UIButton *)sender { sender.backgroundColor = [UIColor redColor]
先了解Hit-Test请看上一篇文章 https://www.jianshu.com/writer#/notebooks/4989829/notes/33140137 定义一个UIButton的Category UIButton+ClickRange.h import <UIKit/UIKit.h> @interface UIButton (ClickRange) /** 自定义响应边界 UIEdgeInsetsMake 3.0 例如:self.btn.hitHeightScale = 3.0; */ @property(nonatomic, assign) CGFloat hitHeightScale; @end UIButton +ClickRange.m #import "UIButton+ClickRange.h" #import <objc/runtime.h> static const char * kHitEdgeInsets kHitWidthScale = "hitWidthScale"; static const char * kHitHeightScale = "hitWidthScale"; @implementation UIButton
---恢复内容开始--- UIButton 既可以显示图片,又可以显示文字,还能随时调整内部位置 系统自带尺寸 storyboard内部调整UIButton属性 状态 监听按钮点击事件 凡是继承自UIControl (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; 自定义UIButton UIButton内部有两个子控件 UILabel和UIImageView 调整控件内部子控件的frame(两种方式) 实现 titleRectForContentRect 和 imageRectForContentRect // 设置imageView的内边距(影响到imageView) @property(nonatomic) UIEdgeInsets imageEdgeInsets; 图片拉伸 UIButton
的使用 http://my.oschina.net/u/1781028/blog/406683 1.1.5 高亮背景色设置 UIButton设置高亮状态下的背景色【原创】 http://blog.it985 .com/11543.html 1,通过按钮的事件来设置背景色 - (void)viewDidLoad { [super viewDidLoad]; UIButton *button1 = [[UIButton alloc] initWithFrame: CGRectMake(50, 200, 100, 50)]; [button1 setTitle: @"button1" *button2 = [[UIButton alloc] initWithFrame: CGRectMake(170, 200, 100, 50)]; [button2 setTitle: (UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐 http://doc.okbase.net/willingYaTou/archive/38295.html 在UIButton
image.png contactAdd infoLight infoDark System detailDisclosure Custom 代码创建UIButton self.btn1Test = UIButton.init(type:UIButtonType.custom) self.btn1Test?.
问题:设置渐变后,发现Button上的字体不显示,想着设置方法在layer加载后,测试发现还是不行,把背景颜色的透明度改为0,发现背景色覆盖住字体。
介绍 在 iOS 26 中,UIButton 迎来了两项非常实用的更新: Liquid Glass 风格配置方法 — 让按钮拥有全新的半透明折射质感,完美融入 iOS 26 的视觉系统。 适配 Liquid Glass UIButton.Configuration 增加了符合 Liquid Glass 风格的配置方法:glass()、clearGlass()、prominentGlass 代码 import UIKit class ViewController: UIViewController { let configs: [UIButton.Configuration] = { [ { // iOS26新增 var config = UIButton.Configuration.glass 代码 import UIKit class ViewController: UIViewController { let button = UIButton(frame: CGRect(x:
imageView.removeSymbolEffect(ofType: .variableColor.reversing) // imageView.removeAllSymbolEffects() } } UIButton import UIKit class ViewController: UIViewController { lazy var button1: UIButton = { let button = UIButton(frame: CGRect(x: 100, y: 0, width: 100, height: 60)) button.setImage(UIImage button.isSymbolAnimationEnabled = true return button }() lazy var button2: UIButton UIAction { _ in } actionHandler.image = UIImage(systemName: "plus") let button = UIButton
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/55212034
iOS的UIButton是一个非常常见而且常用的控件,我们一般用他来实现某个功能的提交以及选择操作。 两个子视图;而有的人则不会用UIButton来实现图文结合的功能。 那么我们是否要放弃UIButton呢? 答案是否定的,其实UIButton本身是可以支持各种图文结合的,既然UIButton上能同时显示图片和文字,那就可以肯定的说UIButton里面本身一定有一个UIImageView和UILabel8子视图 为了方便大家的使用,我把上面的图文结合样式整理成了一个UIButton的分类方法,大家可以直接拷贝使用: 头文件: // // UIButton+ImageTitleStyle.h //
为了解决这个问题 我们写一个UIButton的Category #import <UIKit/UIKit.h> typedef NS_ENUM(NSUInteger,ButtonEdgeInsetsStyle #import "UIButton+EdgeInsets.h" #import <objc/runtime.h> @implementation UIButton (EdgeInsets) - (void ; @property (weak, nonatomic) IBOutlet UIButton *buttonBottom; @property (weak, nonatomic) IBOutlet UIButton *buttonLeft; @property (weak, nonatomic) IBOutlet UIButton *buttonRight; @property (weak, nonatomic ) IBOutlet UIButton *button4; @property (weak, nonatomic) IBOutlet UIButton *button5; @property (weak