iOS - 一个简单的带标题的图标的实现
代码不复杂,直接上代码:
ImageViewButton.h
//
// ImageViewButton.h//
// 带有图片、底部标题或者顶部的按钮
//
// #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface ImageViewButton : UIView /**
* 带有图标标题的按钮
*
* @param frame 大小
* @param image 图标
* @param title 标题
* @param topTitle 标题是否在顶部
*
*/
- (instancetype)initWithFrame:(CGRect)frame image:(NSString *)image title:(NSString *)title topTitle:(BOOL)topTitle; // 按钮点击回调
@property (copy, nonatomic) void (^iconClickCallback) (void); @end NS_ASSUME_NONNULL_END
ImageViewButton.m
//
// ImageViewButton.m
// #import "ImageViewButton.h" @interface ImageViewButton () // 图标
@property (strong, nonatomic) UIButton *iconButton;
// 标题
@property (strong, nonatomic) UILabel *titleLabel;
// 标题是否在顶部
@property (assign, nonatomic) BOOL topTitle; @end @implementation ImageViewButton - (instancetype)initWithFrame:(CGRect)frame image:(NSString *)image title:(NSString *)title topTitle:(BOOL)topTitle {
if (self = [super initWithFrame:frame]) {
self.topTitle = topTitle;
self.translatesAutoresizingMaskIntoConstraints = NO; // 图标
self.iconButton = [[UIButton alloc] initWithFrame:CGRectZero]; self.iconButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.iconButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[self.iconButton addTarget:self action:@selector(iconButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.iconButton]; // 标题
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.text = title; [self addSubview:self.titleLabel]; [self setConstraints];
} return self;
} - (void)iconButtonClick:(UIButton *)sender {
if (self.iconClickCallback) {
self.iconClickCallback();
}
} - (void)setConstraints {
// 标题
NSLayoutConstraint *titleLabelTopBottomLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
NSLayoutConstraint *titleLabelLeadingLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0];
NSLayoutConstraint *titleLabelTrailingLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0];
NSLayoutConstraint *titleLabelHeightLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:16.0]; // 图标
NSLayoutConstraint *iconImageViewWidthHeightLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.iconButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.iconButton attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];
NSLayoutConstraint *iconImageViewCenterXLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.iconButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
NSLayoutConstraint *iconImageViewTopLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.iconButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.titleLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
NSLayoutConstraint *iconImageViewBottomLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.iconButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; if (self.topTitle == NO) {
// 标题在底部
titleLabelTopBottomLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
iconImageViewTopLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.iconButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
iconImageViewBottomLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.iconButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.titleLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
} [self addConstraints:@[titleLabelTopBottomLayoutConstraint, titleLabelLeadingLayoutConstraint, titleLabelTrailingLayoutConstraint, titleLabelHeightLayoutConstraint, iconImageViewWidthHeightLayoutConstraint, iconImageViewCenterXLayoutConstraint, iconImageViewTopLayoutConstraint, iconImageViewBottomLayoutConstraint]];
} /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ @end
iOS - 一个简单的带标题的图标的实现的更多相关文章
- avalon实现一个简单的带增删改查的成绩单
自从angular问世,一直就有去了解学习angular,一直想用angular去做一个项目,但无奈,大ng是国外产物,ng1.2版本就只兼容到IE8,1.3后的几个版本提升到IE9,据说NG2.0更 ...
- iOS一个简单的设置圆角不引起性能问题的分类
http://www.cocoachina.com/articles/18756 iOS设置圆角矩形和阴影效果 https://www.cnblogs.com/rayshen/p/4900336.ht ...
- 一个简单实用的css loading图标
摘要 在web开发中,为了提高用户体验,在加载数据的时候都会给一个loading的提示. Html <!DOCTYPE html> <html xmlns="http:// ...
- 一个简单的带缓存http代理
眼下1.0版模型非常easy.即对客户机发来的请求进行简单处理后,转发到server.转发之前先检查本地缓存.假设有.则直接回送给客户本地资源 程序流程大致例如以下图: 缓存是通过把文件保存到磁盘上, ...
- iOS 一个简单的单例
比如我有一个Singleton的类(DemoStatusManage),他有一个实例方法currentStatus会返回一个1-100的随机数. @interface DemoStatusManage ...
- 从零开始,做一个简单的Vuetify项目,图标安装成功
安装Vuefity的时候,碰到一些坑,经过一番折腾,终于成功,记录正确的姿势如下: 创建一个Vue项目: vue init webpack-simple vular-admin 进入项目目录: cd ...
- 随手撸一个简单的带检查的printf
#include <stdio.h> #include <iostream> #include <vector> #include <string> # ...
- Bootstrap+JSP实例学习笔记一.简单的带登录功能的首页
前言 Bootstrap 是流行的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目.源自于twiteer内部的开发框架. 当前(2019-05)最新版本是v3.3. ...
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...
随机推荐
- ArcGIS Server 10.1新特性系列---动态图层
ArcGIS Server 10.1新特性里面有几个新功能是非常棒的,其中有一个就是动态图层.动态图层不是一种新发明的图层,而是说在arcgis server的web应用中可以动态的配置其渲染和内容功 ...
- python修改列表
替换元素 效果图: 代码: #创建一个列表 list = ['a','b','c','d','e','f'] print('修改前:',list) #修改元素 指定索引重设其值 list[1] = ' ...
- UIBPlayer (视频播放)demo分享
本文出自APICloud官方论坛 UIBPlayer 封装了百度云播放器 SDK.本模块带有UI方案,打开后为一个具有完整功能的播放器界面.百度云播放器突破 Android.iOS 平台对视频格式的限 ...
- 第三篇python用户登录程序实现
需求: 1.通过注册输入用户名和密码 2.能够验证用户名和密码是否正确 3.限制输入一定错误次数后退出程序 4.利用格式化输出方式输出信息 分析: 使用username=input()和passwor ...
- 【红外DDE算法】数字细节增强算法的缘由与效果(我对FLIR文档详解)
[红外DDE算法]数字细节增强算法的缘由与效果(我对FLIR文档详解) 1. 为什么红外系统中图像大多是14bit(甚至更高)?一个红外系统的性能经常以其探测的范围来区别,以及其对最小等效温差指标.首 ...
- [bzoj4447] [loj#2010] [Scoi2015] 小凸解密码
Description 小凸得到了一个密码盘,密码盘被等分成 \(N\) 个扇形,每个扇形上有一个数字(0-9),和一个符号("+"或"*") 密码盘解密的方法 ...
- 双指针,BFS与图论(一)
(一)双指针 1.日志统计 小明维护着一个程序员论坛.现在他收集了一份”点赞”日志,日志共有 N 行. 其中每一行的格式是: ts id 表示在 ts 时刻编号 id 的帖子收到一个”赞”. 现在小明 ...
- Ubuntu下配置Apache以及搭载CGI
在Windows下自己下载应用过Apache,在Linux下也用到了服务器,就选择了Apache.Apache的安装在Ubuntu下异常简单. 1. 上网下载自动包安装 sudo apt-get in ...
- ArcGIS10.2安装教程
1,首先要下载Arcgis 10.2软件,很大大约有2个多G.下载后分别全部解压. 2,首先,安装相应的LicenseManager,双击Arcgis 10.2打开安装界面,选择安装. 3,开始安装L ...
- linux开机无法进入桌面直接进入initramfs模式的问题修复
可能是因为关机异常导致磁盘错误. kali linux升级到2019.4版本之后出现过好几次异常关机导致直接进入initramfs的模式,无法进入系统桌面,网上的办法基本上也都是无效的,前几次翻了很多 ...