iOS 自定义一对UI表现相反的按钮
假如有一对按钮【重置】【提交】,要让他们的默认UI和点击的UI表现刚好相反
【提交】按钮,默认橙色,点击边框是橙色,字体是橙色,背景变白色
【重置】按钮,默认白色橙色,边框是橙色,点击字体是白色,背景变橙色
@implementation SubmmitButton
- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title {
self = [super initWithFrame:frame];
if (self) {
UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
// UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
self = [SubmmitButton new];
[self setTitle:title forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setBackgroundImage:image forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont systemFontOfSize:16]];
[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:6];
}
return self;
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
if (highlighted) {
[self setBackgroundImage:selectedImage forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor orangeColor].CGColor;
}else {
[self setBackgroundImage:image forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
@implementation RestButton
- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title {
self = [super initWithFrame:frame];
if (self) {
// UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
self = [RestButton new];
[self setTitle:title forState:UIControlStateNormal];
[self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[self setBackgroundImage:selectedImage forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont systemFontOfSize:16]];
[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:6];
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor orangeColor].CGColor;
}
return self;
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
if (highlighted) {
[self setBackgroundImage:image forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}else {
[self setBackgroundImage:selectedImage forState:UIControlStateNormal];
[self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor orangeColor].CGColor;
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
UIImage的一个分类方法
/**
根据颜色生图片
@param color 生成图片的颜色
@return 返回纯色图片
*/
+ (UIImage*)createImageWithColor:(UIColor*)color {
CGRect rect = CGRectMake(0.0f,0.0f,10.0f,10.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
return theImage;
}
iOS 自定义一对UI表现相反的按钮的更多相关文章
- iOS 自定义图片和文字垂直显示按钮<上面是图片,文字显示下面>
#import <UIKit/UIKit.h> @interface VerticalButton : UIButton @end #import "VerticalButton ...
- iOS自定义的UISwitch按钮
UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...
- iOS点击获取短信验证码按钮
概述 iOS点击获取短信验证码按钮, 由于 Demo整体测试运行效果 , 整个修改密码界面都已展现, 并附送正则表达式及修改密码逻辑. 详细 代码下载:http://www.demodashi.com ...
- React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发
React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发 2016/09/23 | React Native技术文章 | Sky丶清| 4 条评论 | 1 ...
- iOS自定义转场动画实战讲解
iOS自定义转场动画实战讲解 转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...
- amazeui学习笔记--js插件(UI增强2)--按钮交互Button
amazeui学习笔记--js插件(UI增强2)--按钮交互Button 一.总结 1.按钮loading状态: <button type="button" class=&q ...
- iOS系列 基础篇 06 标签和按钮 (Label & Button)
iOS系列 基础篇 06 标签和按钮 (Label & Button) 目录: 标签控件 按钮控件 小结 标签和按钮是两个常用的控件,下面咱们逐一学习. 1. 标签控件 使用Single Vi ...
- 【iOS自定义键盘及键盘切换】详解
[iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...
- 免费素材下载:iOS 8 矢量 UI 素材套件
小伙伴们,苹果终于在今天凌晨推送了 iOS 8 的正式版.虽然该系统并未与 iPhone6 发布会同时亮相,但对于已经提前体验尝鲜过测试版的同学来说并不陌生.iOS 8 几乎每个图标都进行了重新设计, ...
随机推荐
- git推送新项目到github
1.首先在github上新建一个裸仓库 得到新仓库地址 2.打开本地要添加项目的目录,右键选择git bash,执行命令 (1)git init (2)git remote add origin ht ...
- ubuntu中安装Python3.7
一. 源码安装: 1. 官网源码下载: Python官网:https://www.python.org/downloads/ setuptools官网:https://pypi.org/project ...
- python3的base64编解码
使用python3的base64编解码实现字符串的简易加密解密 引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应 ...
- javaScript运动框架之缓冲运动
缓冲运动 逐渐变慢,最后停止 距离越远速度越大 速度由距离决定 速度=(目标值-当前值)/缩放系数 存在Bug 速度取整 跟随页面滚动的缓冲侧边栏 潜在问题:目标值不是整数时 缓冲运动的停止条件 ...
- python操作redis用法详解
python操作redis用法详解 转载地址 1.redis连接 redis提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用 ...
- usermod 修改用户信息
7.2 usermod 修改用户信息 1.命令功能 usermod 修改已存在的用户账号信息. 2.语法格式 usermod option login 参数选项说明 选项 选项说明 -c 修改用户pa ...
- Codeforces 899 1-N两非空集合最小差 末尾最多9对数计算 pair/链表加优先队列最少次数清空
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- RMQ 2d 模板
#include<iostream> #include<cstdio> #include<string.h> #include<string> #inc ...
- 基本运算符与if while详解:
''' 基本运算符与if while详解: ''' # 算术运算符 # + - * / % // ** # 返回一个数值 # 比较运算符 # > >= < <= == != # ...
- QT中获取选中的radioButton的两种方法
QT中要获取radioButton组中被选中的那个按钮,可以采用两种如下两种办法进行: 方法一:采用对象名称进行获取 代码: 1 QRadioButton* pbtn = qobject_cast&l ...