简单demo
页面效果
选择如下选项创建项目
引入SDAutoLayout
将SDAutoLayout文件夹拖拽至项目中,使其所在位置与AppDelegate.h文件同级
拖拽过程中会出现一个弹框,弹框选项按下图勾选
代码
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 初始化主窗口window并显示在上层
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// 初始化根视图控制器
ViewController *vivRoot = [[ViewController alloc]init];
self.window.rootViewController = vivRoot;
return YES;
}
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
@end
LoginPage.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface LoginPage : UIView
@property (nonatomic, strong) UILabel *nameLabel; // name标签
@property (nonatomic, strong) UITextField *nameText; // 姓名
@property (nonatomic, strong) UILabel *nameContext; // 姓名文本
@property (nonatomic, strong) UIButton *confirmBtn; // 确认按钮
@property (nonatomic, strong) UIButton *resetBtn ; // 重置按钮
@end
NS_ASSUME_NONNULL_END
LoginPage.m
#import "LoginPage.h"
#import "SDAutoLayout.h"
@implementation LoginPage
-(UILabel *)nameLabel{
if (!_nameLabel) {
_nameLabel = [[UILabel alloc]init];
_nameLabel.text = @"姓名:";
_nameLabel.font = [UIFont systemFontOfSize:15 weight:1];
_nameLabel.textColor = [UIColor whiteColor];
}
return _nameLabel;
}
-(UITextField *)nameText{
if (!_nameText) {
_nameText = [[UITextField alloc]init];
_nameText.backgroundColor = [UIColor whiteColor];
_nameText.font = [UIFont systemFontOfSize:14];
}
return _nameText;
}
-(UILabel *)nameContext{
if (!_nameContext) {
_nameContext = [[UILabel alloc]init];
// // 居中
_nameContext.textAlignment = NSTextAlignmentCenter;
_nameContext.font = [UIFont systemFontOfSize:24 weight:1];
_nameContext.textColor = [UIColor whiteColor];
// _nameContext.text = @"HelloWorld";
}
return _nameContext;
}
-(UIButton *)confirmBtn{
if (!_confirmBtn) {
_confirmBtn = [[UIButton alloc]init];
_confirmBtn.backgroundColor = [UIColor redColor];
[_confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
[_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_confirmBtn.titleLabel.font = [UIFont systemFontOfSize:20];
// 绑定事件
[_confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmBtn;
}
-(UIButton *)resetBtn {
if (!_resetBtn) {
_resetBtn = [[UIButton alloc]init];
_resetBtn.backgroundColor = [UIColor redColor];
[_resetBtn setTitle:@"重置" forState:UIControlStateNormal];
[_resetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_resetBtn.titleLabel.font = [UIFont systemFontOfSize:20];
// 绑定事件
[_resetBtn addTarget:self action:@selector(reset) forControlEvents:UIControlEventTouchUpInside];
}
return _resetBtn;
}
// 挂载控件
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.nameLabel];
[self addSubview:self.nameText];
[self addSubview:self.nameContext];
[self addSubview:self.confirmBtn];
[self addSubview:self.resetBtn];
}
return self;
}
// 设置样式
-(void)layoutSubviews
{
[super layoutSubviews];
_nameLabel.sd_layout.leftSpaceToView(self, 50).topSpaceToView(self, 230).heightIs(30);
_nameText.sd_layout.leftSpaceToView(_nameLabel, 10).rightSpaceToView(self, 50).heightIs(30).topSpaceToView(self, 230);
_nameContext.sd_layout.leftSpaceToView(self, 50).rightSpaceToView(self, 50).topSpaceToView(_nameLabel, 20).heightIs(100);
_confirmBtn.sd_layout.leftSpaceToView(self, 50).topSpaceToView(_nameContext, 30).heightIs(50).widthIs(100);
_resetBtn.sd_layout.rightSpaceToView(self, 50).topSpaceToView(_nameContext, 30).heightIs(50).widthIs(100);
self.backgroundColor = [UIColor orangeColor];
}
-(void) confirm{
NSString *nameTextValue = self.nameText.text;
if (nameTextValue) {
NSString *str = [[NSString alloc]initWithFormat:@"你好!%@", nameTextValue];
self.nameContext.text = str;
}
}
-(void) reset {
self.nameText.text = @"";
self.nameContext.text = @"";
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
ViewController.m
#import "ViewController.h"
#import "LoginPage.h"
@interface ViewController ()
@property (nonatomic, strong) LoginPage *loginPage;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createPage];
}
- (void) createPage{
self.loginPage = [[LoginPage alloc]initWithFrame:self.view.bounds];
[self.view addSubview:self.loginPage];
}
@end