美文网首页
iOS屏幕适配(只支持竖屏)

iOS屏幕适配(只支持竖屏)

作者: feng1991 | 来源:发表于2017-01-10 18:02 被阅读0次

接手公司项目,在里面看见了屏幕适配的方法,感觉很方便,缺点就是只支持竖屏。

1.首先在工程里面创建一个pch文件。这个百度一下就行,网址http://www.jianshu.com/p/67ce72c4ad6c
2.再来到appDelegate.h里面生成两个属性。

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (assign, nonatomic) float autoSizeScaleX;
@property (assign, nonatomic) float autoSizeScaleY;

@end

3.来到appDelegate.m里面,先将获得屏幕的宽高写个宏定义,再在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法里面添加下面代码:

#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    
    if(ScreenHeight > 480 && ScreenHeight<1024){
        myDelegate.autoSizeScaleX = ScreenWidth/320;
        myDelegate.autoSizeScaleY = ScreenHeight/568;
    }else if(ScreenHeight >= 1024){
        myDelegate.autoSizeScaleX = ScreenWidth/320;
        myDelegate.autoSizeScaleY = ScreenHeight*1.333333/568;
    }else
    {
        myDelegate.autoSizeScaleX = 1.0;
        myDelegate.autoSizeScaleY = 1.0;
    }

    
    
    return YES;
}

4.在pch文件里面添加AppDelegate的头文件

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#import "AppDelegate.h"

#endif /* PrefixHeader_pch */

5.创建一个Header file文件如下图

424948DD-28F6-4701-B2FB-EB5BC57D2FDD.png

6.在Header file文件里面添加下面代码

#ifndef SetFrame_h
#define SetFrame_h
CG_INLINE CGRect CGRectMakes(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGRect rect;
    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;
}

CG_INLINE CGSize CGSizeMakes(CGFloat width, CGFloat height)
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGSize rect;
    rect.width  = width * myDelegate.autoSizeScaleX;
    rect.height = height * myDelegate.autoSizeScaleY;
    return rect;
}

CG_INLINE CGPoint CGPointMakes(CGFloat width, CGFloat height){
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGPoint rect;
    rect.x  = width * myDelegate.autoSizeScaleX;
    rect.y = height * myDelegate.autoSizeScaleY;
    return rect;
}

CG_INLINE CGFloat TableCellHeight(CGFloat height){
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGFloat newHeight;
    newHeight  = height * myDelegate.autoSizeScaleY;
    return newHeight;
}


#endif /* SetFrame_h */

7.在pch文件里面添加Header file的头文件

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#import "AppDelegate.h"
#import "SetFrame.h"

#endif /* PrefixHeader_pch */

这样就可以使用了,有4个方法可以调用,分别是CGRectMakes(CGFloat x, CGFloat y, CGFloat width, CGFloat height)、CGSizeMakes(CGFloat width, CGFloat height)、CGPointMakes(CGFloat width, CGFloat height)和TableCellHeight(CGFloat height)。使用的时候直接将系统的修改成这是个方法,比如创建一个label,直接将系统的CGRectMake改为CGRectMakes就可以了。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMakes(10, 10,10 , 10)];
    label.backgroundColor = [UIColor redColor];
    [self.view addSubview:label];      
}

相关文章

  • iOS屏幕适配(只支持竖屏)

    接手公司项目,在里面看见了屏幕适配的方法,感觉很方便,缺点就是只支持竖屏。 1.首先在工程里面创建一个pch文件。...

  • 一个能适配所有iphone和ipad屏幕适配工具UIAutoAd

    UIAutoAdaptKit 根据iOS设备屏幕尺寸比例计算调整 支持横竖屏屏幕适配 支持cocoapods : ...

  • iOS的横、竖屏和旋转

    前言: iOS App中大多数页面是只展示竖屏下的效果,但是少部分页面需要支持横竖屏。本文分别介绍监听屏幕旋转方向...

  • iOS开发屏幕适配

    Hello,简书! ## iOS开发屏幕适配 ## 屏幕适配原则大屏手机显示更多的内容 - 并不是大屏手机就根据屏...

  • iOS 只支持横屏/竖屏

    写在前面 今天在写一个可以制定计划的小应用,我希望在iPhone上只支持横屏,百度搜了一下,杂七杂八的,还是自己研...

  • 网格的创建和适配

    网格的适配横屏竖屏 注意适配加入iOS第三方 Masonry 图片定义从零开始 //AppDelegate.m #...

  • 【iOS开发】在特定的View Controller允许屏幕旋转

    目前我们常用的iOS应用,大部分都是只支持竖屏。但是在某些View Controller中,我们想让应用支持横屏。...

  • 横竖屏

    //如果你要支持不同的屏幕方向,需要这样设置,否则在iPhone只支持一个竖屏方向 [UMSocialConfig...

  • iOS各种屏幕自动适配及颜色转换为十六进制

    iOS各种屏幕自动适配及颜色转换为十六进制 ★★★XLJMatchScreen自动适配屏幕★★★ 支持pod导入 ...

  • 横竖屏切换 (swift)

    一. 需求 APP中需要支持横屏和竖屏,并在不同的页面 可支持的屏幕旋转方向不一致 整体竖屏,部分强制横屏 整体横...

网友评论

      本文标题:iOS屏幕适配(只支持竖屏)

      本文链接:https://www.haomeiwen.com/subject/vvakbttx.html