欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS 适配iPhone X的方法

程序员文章站 2024-02-11 19:34:58
因为iphone x奇特的刘海存在,ios11之后系统深化了“安全区域”概念,安全区域就是从屏幕上切除最大的矩形之外的区域。 ios11后uiscrollview新增co...

因为iphone x奇特的刘海存在,ios11之后系统深化了“安全区域”概念,安全区域就是从屏幕上切除最大的矩形之外的区域。

ios11后uiscrollview新增contentinsetadjustmentbehavior属性,默认配置uiscrollviewcontentinsetadjustmentautomatic,效果上就是没使用安全区域。若针对具体页面需要使用安全区域,可以查看api中新增加的那些属性。

/** 
 * 适配iphone x的安全区域 
 * isuse = 1 表示使用安全区域 
 * isuse = 0 表示不使用安全区域 
 */ 
+ (void)adaptationsafeareawith:(uiscrollview *)sv usearea:(nsinteger)isuse { 
  if ([[sv class] issubclassofclass:[uiwebview class]]) { 
    uiwebview *webview = (uiwebview *)sv; 
    for (uiview *aview in [webview subviews]) { 
      if ([aview iskindofclass:[uiscrollview class]]) { 
        sv = (uiscrollview *)aview; 
        break; 
      } 
    } 
  } 
#ifdef __iphone_11_0 
  if ([sv respondstoselector:@selector(setcontentinsetadjustmentbehavior:)]) { 
    if (isuse) { 
      if (@available(ios 11.0, *)) { 
        sv.contentinsetadjustmentbehavior = uiscrollviewcontentinsetadjustmentnever; 
        if ([[sv class] issubclassofclass:[uitableview class]]) { 
          uitableview *tv = (uitableview *)sv; 
          [tv setinsetscontentviewstosafearea:no]; 
        } 
      } else { 
        // fallback on earlier versions 
      } 
    } else { 
      if (@available(ios 11.0, *)) { 
        sv.contentinsetadjustmentbehavior = uiscrollviewcontentinsetadjustmentalways; 
      } else { 
        // fallback on earlier versions 
      } 
    } 
  } 
#endif 
} 
typedef ns_enum(nsinteger, uiscrollviewcontentinsetadjustmentbehavior) { 
  uiscrollviewcontentinsetadjustmentautomatic,   // similar to .scrollableaxes, but for backward compatibility will also adjust the top & bottom contentinset when the scroll view is owned by a view controller with automaticallyadjustsscrollviewinsets = yes inside a navigation controller, regardless of whether the scroll view is scrollable 
  uiscrollviewcontentinsetadjustmentscrollableaxes, // edges for scrollable axes are adjusted (i.e., contentsize.width/height > frame.size.width/height or alwaysbouncehorizontal/vertical = yes) 
  uiscrollviewcontentinsetadjustmentnever,     // contentinset is not adjusted 
  uiscrollviewcontentinsetadjustmentalways,     // contentinset is always adjusted by the scroll view's safeareainsets 
} api_available(ios(11.0),tvos(11.0)); 

以上这篇ios 适配iphone x的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。