美文网首页
同时适配iPhone App和 iPad APP

同时适配iPhone App和 iPad APP

作者: yalahabawa | 来源:发表于2020-04-10 00:15 被阅读0次

    如果要同时适配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)];
            }];
        }
    

    相关文章

      网友评论

          本文标题:同时适配iPhone App和 iPad APP

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