UI中 用单例的方法进行屏幕支配,由相对位置得到确定视图位置

#import <UIKit/UIKit.h>

//获取手机屏幕长和宽

#define  ScreenHEIGHT [[UIScreen mainScreen]bounds].size.height

#define  ScreenWIDTH [[UIScreen mainScreen]bounds].size.width

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

//横坐标缩放

@property(assign,nonatomic)float autoSizeScaleX;

//纵坐标缩放

@property(assign,nonatomic)float autoSizeScaleY;

@end

--------------------------------------------------------------------------------

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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

    

    //iphon4屏幕高度480

    if(ScreenHEIGHT>480)

    {

        //以iphon5为例  得到屏幕比例

        myDelegate.autoSizeScaleX=ScreenWIDTH/320;

        myDelegate.autoSizeScaleY=ScreenHEIGHT/568;

        

    }else

    {

        myDelegate.autoSizeScaleX=1.0;

        myDelegate.autoSizeScaleY=1.0;

    }

    

    

    return YES;

}

 -----------------------------------------------------------------------------------------------

#import "ViewController.h"

#import "AppDelegate.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake1(100, 200, 200, 44)];

    lbl.backgroundColor=[UIColor redColor];

    [self.view addSubview:lbl];

  

}

//内联函数

CG_INLINE CGRect

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

{

    CGRect rect;

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

    

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

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

    

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

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

    

    return rect;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

 这样显示出来的视图,无论在iphon任何版本中都按比例显示,而不会乱。

原文地址:https://www.cnblogs.com/wrzheng/p/5360866.html