屏幕适配 和等比缩放

#import <UIKit/UIKit.h>

#define WIDTH self.view.frame.size.width

#define HEIGHT self.view.frame.size.height

@interface ViewController : UIViewController<UITextFieldDelegate>

@property(strong,nonatomic)UIImageView *imageV;

@property(strong,nonatomic)UITextField *txtID;

@property(strong,nonatomic)UITextField *txtmima;

@property(strong,nonatomic)UIButton *btnlogin;

@property(strong,nonatomic)UIButton *btnregister;

@property(strong,nonatomic)UILabel *label;

@property(strong,nonatomic)UIButton *btn1;

//

//  ViewController.m

//  屏幕适配

//

//  Created by tlh on 16/4/6.

//  Copyright © 2016年 lamco. All rights reserved.

//

#import "ViewController.h"

#import "UIViewExt.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

   

    self.imageV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

    self.imageV.image=[UIImage imageNamed:@"beijing.png"];

    [self.view addSubview:self.imageV];

   

    self.txtID=[[UITextField alloc]initWithFrame:CGRectMake(self.view.left+90, self.view.top + 150, WIDTH/2, HEIGHT/20)];

//    self.txtID.backgroundColor=[UIColor whiteColor];

//    self.txtID.borderStyle=0;

//    self.txtID.placeholder=@"  请输入账号" ;

//    设置占位符及其字体颜色

    self.txtID.delegate=self;

    UIColor *color1=[UIColor colorWithWhite:1.0 alpha:0.5];

    self.txtID.attributedPlaceholder=[[NSAttributedString alloc]initWithString:@"请输入账号" attributes:@{NSForegroundColorAttributeName:color1}];

    UIImageView *imagev=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"phoneIcon@x.png"]];

    self.txtID.clearButtonMode=YES;

    

    self.txtID.leftView=imagev;

    self.txtID.leftViewMode=UITextFieldViewModeAlways;

    

     self.label=[[UILabel alloc]initWithFrame:CGRectMake(self.view.left+90, self.txtID.bottom, WIDTH/2, 1.0)];

    self.label.backgroundColor=[UIColor whiteColor];

    [self.view addSubview: self.label];

    [self.view addSubview:self.txtID];

    

    

    self.txtmima=[[UITextField alloc]initWithFrame:CGRectMake(self.view.left+90, self.txtID.bottom+20, WIDTH/2, HEIGHT/20)];

//    self.txtmima.placeholder=@"  请输入密码";

    self.txtmima.delegate=self;

    UIColor *color=[UIColor colorWithWhite:1 alpha:0.5];

    self.txtmima.attributedPlaceholder=[[NSAttributedString alloc]initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName:color}];

    self.txtmima.secureTextEntry=YES;

    UIImageView *imagev1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"passwordIcon@x.png"]];

    self.txtmima.leftView=imagev1;

    self.txtmima.leftViewMode=UITextFieldViewModeAlways;

    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(self.view.left+90, self.txtmima.bottom, WIDTH/2, 1.0)];

    lbl.backgroundColor=[UIColor whiteColor];

    [self.view addSubview:lbl];

    [self.view addSubview:self.txtmima];

    //    使密码可见

    self.btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [self.btn1 setBackgroundImage:[UIImage imageNamed:@"btn_password_hidden@3x.png"] forState:UIControlStateNormal];

    [self .btn1 addTarget:self action:@selector(display) forControlEvents:UIControlEventTouchUpInside];

    

    self.btn1.frame=CGRectMake(0, 0, self.txtmima.width/12, self.txtmima.size.height);

    self.txtmima.rightView=self.btn1 ;

    self.txtmima.rightViewMode= UITextFieldViewModeAlways;

//    [self.txtmima addSubview:self.btn1];

    

//    登陆按钮

    self.btnlogin=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.btnlogin.frame=CGRectMake(self.view.left+90, self.txtmima.bottom+50, WIDTH/2+imagev.size.width, HEIGHT/19);

    [self.btnlogin setBackgroundImage:[UIImage imageNamed:@"loginButton@x.png"] forState:UIControlStateNormal];

    

    [self.btnlogin setTitle:@"登陆" forState:UIControlStateNormal];

    [self.btnlogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [self.view addSubview:self.btnlogin];

//     注册按钮

    self.btnregister=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.btnregister.frame=CGRectMake(self.view.left+90, self.btnlogin.bottom+20, WIDTH/2+imagev.size.width, HEIGHT/19);

    [self.btnregister setBackgroundImage:[UIImage imageNamed:@"rigisterButton@2x.png"] forState:UIControlStateNormal];

    [self.btnregister setTitle:@"注册" forState:UIControlStateNormal];

    [self.btnregister setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];

    [self.view addSubview:self.btnregister];

    

//    welcome 图片

    UIImageView *imagev2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"welcome@2.png"]];

    imagev2.frame=CGRectMake(self.view.left+90, self.view.top+70, WIDTH/2, HEIGHT/19);

    [self.view addSubview:imagev2];

//   logo图片

    UIImageView *imagev3=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo@2x.png"]];

    imagev3.frame=CGRectMake(self.view.left+WIDTH/2-50, self.view.bottom-100, WIDTH/4, HEIGHT/18);

    [self.view addSubview:imagev3];

    

//    self.btnlogin=[UIButton buttonWithType:UIButtonTypeRoundedRect];

//    self.btnlogin.frame=CGRectMake(self.view.left+50, self.txtmima.bottom+50, WIDTH/3, HEIGHT/7);

//    

}

//键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [self.txtID resignFirstResponder];

    [self.txtmima resignFirstResponder];

    return YES;

}

-(void)display

{

    if (self.txtmima.secureTextEntry==NO) {

        [self.btn1 setBackgroundImage:[UIImage imageNamed:@"btn_password_hidden@3x.png"] forState:UIControlStateNormal];

        self.txtmima.secureTextEntry=YES;

    }

    else

    {

        [self.btn1 setBackgroundImage:[UIImage imageNamed:@"btn_password_show@2x.png" ] forState:UIControlStateNormal];

        self.txtmima.secureTextEntry=NO;

    }

}

@property(strong,nonatomic)UITextField *txtName;

//  ViewController.m

//  等比缩放

//

//  Created by tlh on 16/4/6.

//  Copyright © 2016年 lamco. All rights reserved.

//

#import "ViewController.h"

#import "AppDelegate.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.txtName=[[UITextField alloc]initWithFrame:CGRectMake1(50, 100, 200, 55)];

    self.txtName.backgroundColor=[UIColor redColor];

    self.txtName.borderStyle=1;

    [self.view addSubview:self.txtName];

    

}

CG_INLINE CGRect

CGRectMake1(CGFloat x,CGFloat y,CGFloat width,CGFloat height)

{

    CGRect rect;

    AppDelegate *myDelegate=[[UIApplication sharedApplication] delegate];

    rect.origin.x=x * myDelegate.autoSizeScaleX;

    rect.origin.y=y * myDelegate.autoSizeScaleY;

    rect.size.width=width *myDelegate.autoSizeScaleX;

    rect.size.height=height *myDelegate.autoSizeScaleY;

    

    return rect;

}

原文地址:https://www.cnblogs.com/tianlianghong/p/5361279.html