iOS development · Macros and methods related to iPhone X adaptation

iOS development · Macros and methods related to iPhone X adaptation

After a long time, today I finally have time to summarize the pitfalls related to adapting to iPhone X. Generally speaking, there are two kinds of pitfalls. One is that the height of the navigation bar + status bar has changed, and the other is that some UITableViews that do not implement proxy methods such as -tableView: viewForHeaderInSection: and -tableView: viewForFooterInSection: will be misplaced.

1. Determine whether it is an iPhone X: Return YES or NO

1.1 Judgment: Macro

(1) Based on screen resolution

Trinocular algorithm

  1. //Is it iPhoneX YES: iPhoneX screen NO : Traditional screen
  2. #define kIs_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode]. size ) : NO )

Multi-line logical judgment

  1. //Whether it is iPhoneX 1: iPhoneX screen 0: Traditional screen
  2. #define kIs_iPhoneX_test ({\
  3. int tmp = 0;\
  4. if ([UIScreen instancesRespondToSelector:@selector(currentMode)]) {\
  5. if (CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode]. size )) {\
  6. tmp = 1;\
  7. } else {\
  8. tmp = 0;\
  9. }\
  10. } else {\
  11. tmp = 0;\
  12. }\
  13. tmp;\
  14. })

Among them, the backslash \ is not a comment or other useless symbol, but a necessary symbol for multi-line macro line breaks.

***The sentence tmp;\ is also necessary, because the tmp obtained through logical judgment is used as the return value of the macro.

(2) Based on screen size

  1. #define kIs_iPhoneX (kSCREEN_WIDTH == 375.f && kSCREEN_HEIGHT == 812.f)
  2. #define kSCREEN_WIDTH ([UIScreen mainScreen].bounds. size .width)
  3. #define kSCREEN_HEIGHT ([UIScreen mainScreen].bounds. size .height)

1.2 Judgment: Method

Method: According to the device model

  1. +(BOOL)getIs_iPhoneX{
  2. struct utsname systemInfo;
  3. uname(&systemInfo);
  4. NSString *platform = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];
  5.       
  6. if([platform isEqualToString:@ "iPhone10,3" ]||[platform isEqualToString:@ "iPhone10,6" ]) {
  7. return YES;
  8. } else {
  9. return   NO ;
  10. }
  11. }

2. Flexible return status bar + navigation bar height

Requirement: Flexibly obtain the height of the navigation bar + status bar as the starting point of the Y axis of a subview.

Macro Definition

  1. #define kStatusBarAndNavigationBarHeight (kIs_iPhoneX ? 88.f : 64.f)

Calling Example

  1. //Automatic adaptation
  2. _segmentedControl.frame = CGRectMake(0, kStatusBarAndNavigationBarHeight, kSCREEN_WIDTH, 55);

3. Extension: Get iOS system and App version information

Get iOS system version number: Returns a string

  1. + (NSString *)getSystemVersion{
  2. return [[UIDevice currentDevice] systemVersion];
  3. }

Get App version number: Returns a string

  1. + (NSString *)getAppVersion{
  2. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  3. // Get the App version number
  4. NSString *appVersion = [infoDic objectForKey:@ "CFBundleShortVersionString" ];
  5. return appVersion;
  6. }

4. Other issues related to iPhone X adaptation

In the process of adapting to iPhone X and Xcode 9, in addition to problems related to the navigation bar, another problem that often occurs is the problem related to UITableView. The following two methods can solve most of the misalignment problems.

When VC creates tableView properties, set it like this

  1. self.tableView.estimatedRowHeight = 0;
  2. self.tableView.estimatedSectionHeaderHeight = 0;
  3. self.tableView.estimatedSectionFooterHeight = 0;

You can also set

  1. //cell adaptive height
  2. self.tableView.rowHeight = UITableViewAutomaticDimension;
  3. //Estimated row height
  4. self.tableView.estimatedRowHeight = 44.0f;

About the safe area of ​​​​the root view

iOS has added a safeArea. In the old code, the code that specifies the relationship between the subview and the root subview needs to add a judgment: when iOS 11 is released, it needs to be changed to the relationship between the subview and the root subview's safe area. In this way, there will be no interference from any controls on the virtual home at the bottom of the iPhone X.

  1. if (@available(iOS 11.0, *)) {
  2. make.edges.equalTo( self.view.safeAreaInsets )
  3. } else {
  4. make.edges.equalTo( self.view )
  5. }

Of course, except for the tabbar, other views such as tableView or web page view can be placed in the bottom virtual home area. At this time, there is no need to emphasize that the subview must be placed in the safeArea, and the original old code does not need to be changed.

<<:  [Live] Technology or management, how should programmers plan their career path?

>>:  After another year of tinkering, what is the dilemma facing WeChat Mini Programs?

Recommend

Product operation strategy: How to carry out user stratification operations?

Pyramid stratification is a method of stratifying...

Google releases first developer preview of Android 12

Google has released the first developer preview o...

Oracle: New TikTok board will have 4 Americans, possibly including Masayoshi Son

On Wednesday local time, Maria Bartiromo, anchor ...

How to choose the promotion channel for APP?

After the APP is launched, promoting the product ...

What? The phoenix is ​​the ancestor of the peacock!

Speaking of the phoenix, many words always come t...

Mid-Autumn Festival Special: I want to go home and see ____!

Traveling across the world See the vast world But...

A must-have mobile browser for experienced drivers: Chrome can be uninstalled!

[[313579]] I believe that everyone is already qui...

A collection of advertising creation tips that took 8 years to compile

Donald Gunn is the creative resources director at...

Progress bar! It's actually! Fake! What's the point of it?

In 1984, Jobs, who was only 29 years old, decided...

How to write soft advertising to convince your users?

As a self-media , one of the ways to monetize is ...