IOS 纯代码自定义UIView案例

objectivec 复制代码
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface OrderAfterPeriodSelectNumView : UIView
//左边标题
@property (nonatomic,strong) UILabel *titleLab;
//数量
@property (nonatomic,strong) UILabel *numLab;

@end
objectivec 复制代码
#import "OrderAfterPeriodSelectNumView.h"
@interface OrderAfterPeriodSelectNumView()
@property (nonatomic, assign) CGFloat titleLabWidth;
@end

@implementation OrderAfterPeriodSelectNumView

#pragma mark - 原始初始化方法

// 1、首先调用init方法
- (instancetype)init{
    if (self = [super init]) {
        [self initUI];
    }
    return self;
}
  
// 2、然后调用initWithFrame方法
- (instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}
// 初始化UI控件
- (void) initUI{
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.textColor = [UIColor colorWithHexString:@"333333"];
    titleLab.font = [UIFont systemFontOfSize:14];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.text = @"退款数量";
    [self addSubview:titleLab];
    _titleLab = titleLab;
    
    UILabel *numLab = [[UILabel alloc] init];
    titleLab.textColor = [UIColor colorWithHexString:@"333333"];
    numLab.font = [UIFont systemFontOfSize:14];
    numLab.textAlignment = NSTextAlignmentRight;
    numLab.text = @"0";
    [self addSubview:numLab];
    _numLab = numLab;
    //获取字符串的宽高
    CGRect rect = [titleLab.text boundingRectWithSize:CGSizeMake(SCREENWIDTH, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]} context:nil];
    _titleLabWidth = rect.size.width;

}
// 设置UI控件的frame
- (void)layoutSubviews{
    [super layoutSubviews];
    // 1.获取当前控件的尺寸
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    // 2.设置子控件的frame
    self.titleLab.frame = CGRectMake(16, 0, self.titleLabWidth, height);
    self.numLab.frame = CGRectMake(16+self.titleLabWidth, 0, width - (16+self.titleLabWidth+16), height);
    
}

@end

添加到UIStackView

objectivec 复制代码
OrderAfterPeriodSelectNumView *numView = [[OrderAfterPeriodSelectNumView alloc] init];
[numView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.height.equalTo(@(50));
 }];
self.numView = numView;
[self.stackView addArrangedSubview:numView];
相关推荐
00后程序员张1 小时前
python 抓包在实际项目中的合理位置,结合代理抓包、设备侧抓包与数据流分析
android·ios·小程序·https·uni-app·iphone·webview
FreeBuf_2 小时前
认证噩梦:新型MacSync窃密木马绕过Gatekeeper劫持Mac设备
macos
绝世唐门三哥9 小时前
Mac 免费 GIF 录制软件全攻略:下载、安装与使用指南
macos·gif
2501_915918419 小时前
使用 HBuilder 上架 iOS 应用时常见的问题与应对方式
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074711 小时前
iOS 崩溃日志的分析方法,将崩溃日志与运行过程结合分析
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074711 小时前
React Native 混淆在真项目中的方式,当 JS 和原生同时暴露
javascript·react native·react.js·ios·小程序·uni-app·iphone
雪域迷影11 小时前
macOS系统上或首次使用Python的urllib模块时出现 ssl.SSLCertVerificationError 错误
python·macos·ssl
00后程序员张12 小时前
苹果应用商店上架App流程,签名证书、IPA 校验、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074712 小时前
iOS 上架需要哪些准备,围绕证书、描述文件和上传方式等关键环节展开分析
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063212 小时前
iOS 上架费用解析,哪些成本可以通过流程优化降低。
android·ios·小程序·https·uni-app·iphone·webview