常见控件布局-懒加载模版
2020-08-14 本文已影响0人
一代千禧
1、UILabel
- (UILabel *)indexLbl{
if (!_indexLbl) {
_indexLbl = [UILabel new];
_indexLbl.font = HYUniFont3;
_indexLbl.textColor = HYUniTextcor1;
}
return _indexLbl;
}
1、UIImageView
- (UIImageView *)activityImageView{
if (!_activityImageView) {
_activityImageView = [UIImageView new];
_activityImageView.contentMode = UIViewContentModeScaleAspectFill;
_activityImageView.image = [UIImage imageNamed:@"hardware_img_update2"];
}
return _activityImageView;
}
//设置网络图片
NSString *urlStr = [HYHardwareUtil imageUrlByModel:deviceModel];
[self.deviceImageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
1、UIView
- (UIView *)lineView{
if (!_lineView) {
_lineView = [UIView new];
_lineView.backgroundColor = [UIColor colorWithHexValue:0xE4E4E4];
_lineView.layer.cornerRadius = 5.0f;
_lineView.clipsToBounds = NO;
}
return _lineView;
}
1、UIButton
#import <SDWebImage/UIButton+WebCache.h>
- (UIButton *)managerFamilyBtn{
if (!_managerFamilyBtn) {
_managerFamilyBtn = [UIButton new];
_managerFamilyBtn.backgroundColor = [UIColor whiteColor];
_managerFamilyBtn.tag = 101;
[_managerFamilyBtn setImage:[UIImage imageNamed:@"nav_icon_set_black_nor"] forState:UIControlStateNormal];
[_managerFamilyBtn setTitle:@"家庭管理" forState:UIControlStateNormal];
_managerFamilyBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
[_managerFamilyBtn.titleLabel setFont:HYHwUniFont17];
[_managerFamilyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_managerFamilyBtn addTarget:self action:@selector(buttonOnclick:) forControlEvents:UIControlEventTouchUpInside];
_managerFamilyBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
_managerFamilyBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 16, 0, 0);
_managerFamilyBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 22, 0, 0);
_managerFamilyBtn.layer.cornerRadius = 4.0;
_managerFamilyBtn.layer.masksToBounds = YES;
}
return _managerFamilyBtn;
}
//设置网络图片
[_iconBtn sd_setImageWithURL:imgurl forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:normalImage] options:SDWebImageRefreshCached];
1、UIButton上下左右设置
//
// ViewController.m
// QiButton_UIEdgeInsets
//
// Created by QiShare on 2018/8/7.
// Copyright © 2018年 QiShare. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self demo1];
[self demo2];
[self demo3];
[self demo4];
}
#pragma mark - 默认Button(作对比用)
- (void)demo1 {
UIButton *button = [self createButtonWithFrame:CGRectMake(50.0, 250.0, 120.0, 120.0)];
[self.view addSubview:button];
}
#pragma mark - 设置imageView与label的距离
- (void)demo2 {
UIButton *button = [self createButtonWithFrame:CGRectMake(200.0, 250.0, 120.0, 120.0)];
button.titleEdgeInsets = UIEdgeInsetsMake(.0, 10.0, .0, .0);
[self.view addSubview:button];
}
#pragma mark - 设置图片在右,文字在左
- (void)demo3 {
UIButton *button = [self createButtonWithFrame:CGRectMake(50.0, 400.0, 120.0, 120.0)];
button.titleEdgeInsets = UIEdgeInsetsMake(.0, - button.imageView.bounds.size.width - 10.0, .0, button.imageView.bounds.size.width);
button.imageEdgeInsets = UIEdgeInsetsMake(.0, button.titleLabel.bounds.size.width, .0, - button.titleLabel.bounds.size.width);
[self.view addSubview:button];
}
#pragma mark - 设置图片在上,文字在下
- (void)demo4 {
UIButton *button = [self createButtonWithFrame:CGRectMake(200, 400, 120.0, 120.0)];
button.titleEdgeInsets = UIEdgeInsetsMake(button.imageView.frame.size.height + 10.0, - button.imageView.bounds.size.width, .0, .0);
button.imageEdgeInsets = UIEdgeInsetsMake(.0, button.titleLabel.bounds.size.width / 2, button.titleLabel.frame.size.height + 10.0, - button.titleLabel.bounds.size.width / 2);
[self.view addSubview:button];
}
#pragma mark - button初始化方法
- (UIButton *)createButtonWithFrame:(CGRect)frame {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = frame;
button.backgroundColor = [UIColor whiteColor];
button.titleLabel.font = [UIFont systemFontOfSize:14.0];
button.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
button.imageView.layer.cornerRadius = 22.0;
button.imageView.layer.masksToBounds = YES;
button.layer.borderColor = [UIColor blackColor].CGColor;
button.layer.cornerRadius = frame.size.width / 2;
button.layer.borderWidth = 1.0;
button.layer.masksToBounds = YES;
[button setTitle:@"QiShare" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"logo"] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
return button;
}
@end
- (void)showSheetView {
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow addSubview:self.backView];
[self.backView addSubview:self];
self.userInteractionEnabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.userInteractionEnabled = YES;
});
[self observerLogoutNotiRemoveViewWithSelector:@"hideSheetView"];
self.deviceType = self.deviceType?:self.guideModel.deviceTypeId;
}
- (void)hideSheetView {
[self removeFromSuperview];
[self.backView removeFromSuperview];
}