zoukankan      html  css  js  c++  java
  • iOS 屏幕适配

    对于iOS开发,屏幕的适配真的很重要,在这里就不多说了,今天主要给大家介绍一下按比例适配;

    1.首先我们在 AppDelegate.h 里面定义两个宏定义,屏幕的宽和高

    #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 (nonatomic ,assign) float autoSizeScaleX;

    @property (nonatomic ,assign) float autoSizeScaleY;

     @end

     2.在AppDelegate.m文件里面实现了计算缩放屏幕的的倍数:

    #import "AppDelegate.h"

     @interface AppDelegate ()

     @end

    @implementation AppDelegate

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

        // Override point for customization after application launch.

        //创建一个UIApplication的单例,并且指定代理,这样就可以传值了

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

        //判断屏幕的高度如果 大于480 ,注意这里是以5s为基准

        if (ScreenHEIGHT >480) {

            //计算出自动缩放的倍数

            myDelegate.autoSizeScaleX = ScreenWIDTH/320;

            myDelegate.autoSizeScaleY = ScreenHEIGHT/568;

        }

        else{

            //小于480 就是iPhoneon4,所以正常显示

            myDelegate.autoSizeScaleX = 1.0;

            myDelegate.autoSizeScaleY = 1.0;

        }

        return YES;

    }

     3.在要屏幕适配的页面加上一个 CG_INLINE CGRect(内联函数):

    #import "ViewController.h"

    #import "AppDelegate.h"

    @interface ViewController ()

     @end

     @implementation ViewController

     - (void)viewDidLoad {

        [super viewDidLoad];

        }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    //内联函数

    CG_INLINE CGRect

    // 重新定义控件的尺寸

       CGRentMake1(CGFloat x, CGFloat y, CGFloat width, CGFloat height){

           CGRect rect;

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

           //按比例缩放 坐标,和尺寸

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

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

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

           rect.size.height = width*myappdelegate.autoSizeScaleY;

           return rect;

    }

    @end

     4.写完内联函数后,以后你定义的控件的frame 都不要用CGRectMake来设置尺寸了,用重新定义控件的CGRentMake1来设置,这样就实现了自动缩放了,可以任性在

    iphone任意尺寸上用了。赶快试试吧!

     注意:此方法只适用于精度不是要求高的应用;

  • 相关阅读:
    4 Python+Selenium的元素定位方法(link/partial link)
    3 Python+Selenium的元素定位方法(id、class name、name、tag name)
    2 Selenium3.0+Python3.6环境搭建
    1 Selenium打开浏览器
    目录处理文件&链接命令
    DOS批处理命令-@命令
    DOS批处理命令-echo
    吐槽一二三
    编码神器之sublime(插件安装)
    两天来学习C的感受
  • 原文地址:https://www.cnblogs.com/liumu/p/5361486.html
Copyright © 2011-2022 走看看