同时适配iPhone App和 iPad APP

2020-04-10  本文已影响0人  yalahabawa

如果要同时适配iPhone App和 iPad APP,首先我们要先获取到用户的机型:
通过下面的方法判断用户使用的机型是iPhone还是iPad:

#define iPhoneDevice (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define iPadDevice (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

由于iPhone和 iPad屏幕的长宽比不一样,iPhone可以根据屏幕的宽度做比例适配,但是iPad不可以,大部分是通过设置控件的上下左右的间距才达到更好的效果(例如使用Masonry)。

例子:(不太好的例子)
//判断为iPad Pro (全面屏)
if (iPadDevice && [UIApplication sharedApplication].statusBarFrame.size.height > 20) {
        [UIView animateWithDuration:0.2 animations:^{
            [self setFrame:CGRectMake(5, 27, 100, 100)];
        }];
//判断为iPad  (矩形屏)
 } else if(iPadDevice && [UIApplication sharedApplication].statusBarFrame.size.height <= 20) {
        [UIView animateWithDuration:0.2 animations:^{
            [self setFrame:CGRectMake(5, 7, 100, 100)];
        }];
//判断为iPhone
 } else {
        [UIView animateWithDuration:0.2 animations:^{
            [self setFrame:CGRectMake(5, 0, 100, 100)];
        }];
    }
上一篇下一篇

猜你喜欢

热点阅读