Summary of WeChat Mini Programs Adapted to iPhone X

Summary of WeChat Mini Programs Adapted to iPhone X

This article mainly introduces some problems and summaries of our Knowledge Collection applet when adapting to the iPhone X screen. I hope it will be helpful for your applet development.

iPhone X screen data

After the official release of iPhone X in September last year, it triggered a wave of iOS App adaptation and technical articles. Here we briefly summarize the basic data of iPhone X screen to facilitate subsequent adaptation in mini program development.

  • Screen size: 5.8 inches (diagonal)
  • Screen physical resolution: 1125px × 2436px, 458 ppi
  • The actual development adaptation size: 375pt × 812pt, @3x

In addition, since there is a "bangs" in the status bar area at the top of the iPhone X screen, and an "operation bar" is added at the bottom of the screen, as shown in the figure below, we need to pay attention to the issue of safe area during development.

[[238031]]

According to Apple's official documentation, the height of the adaptation safety area of ​​the top status bar of iPhone X is 44pt, and the height of the bottom operation bar area is 34pt. In addition, in iPhone X, the default height of some system bars has also changed compared to previous devices, as shown in the following table.

Therefore, in iOS App development, if we use the default UINavigationController and UITabBarController, there is no need for additional adaptation, as the iOS system will automatically adapt the safety area of ​​the relevant bars. If we use a custom navigation bar and tab bar, we need to set the different heights of these bars according to the device type.

Size units of mini programs

In order to solve the problem of adapting to different screen sizes, the mini program has set its own size unit: rpx (responsive pixel), which can adapt to the screen width. The mini program stipulates that the screen width of all devices is 750rpx. Depending on the actual width of the device screen, the actual pixel value represented by 1rpx is also different.

According to WeChat development documentation, on a 4.7-inch iPhone device (iPhone 6/7/8), the screen width is 375px (here it should be understood as 375 points), with a total of 750 physical pixels, then 750rpx = 375px = 750 physical pixels, 1rpx = 0.5px = 1 physical pixel.

How to identify the device as iPhone X in the applet

Above we briefly introduced the screen data of iPhone X and the size units in the mini program as a prelude. Now we are finally going to get to the point. To adapt the iPhone X screen in the mini program, we first need to know how to determine the device type.

WeChat's mini program API provides a wx.getSystemInfo(OBJECT) method to obtain the system information and device information of the user's phone, including the following data:

The meaning of each of the above fields is detailed in the document, and we will not go into details one by one.

Therefore, we can determine whether the device is an iPhone X based on whether the phone model field model returned by this method contains the iPhone X string, or we can determine whether the screenHeight is equal to 812.

NOTE: There is a small pitfall here that needs to be noted. In the simulator of WeChat developer tools, if iPhone X is selected, the model value obtained at this time is iPhone X, which makes me think that the real machine also has this value, so I directly use if (model == 'iPhone X') to judge, but in fact the model value of the real machine is in this format: iPhone X (GSM+CDMA), so we need to use string search matching to make a judgment.

In summary, we can add a field isIPX in globalData of app.js to identify whether the current device is iPhone X, and then call the wx.getSystemInfo(OBJECT) method in onLaunch when the applet is started and read the model field in its success callback for analysis. The code is as follows:

  1. App({
  2. // Global data
  3. globalData: {
  4. // Other data definitions ...
  5.      
  6. isIPX: false , // Is the current device an iPhone X?
  7. },
  8.    
  9. // Mini program startup entry
  10. onLaunch: function (options) {
  11. // Other startup code...
  12.      
  13. // Determine if the device is an iPhone X
  14. this.checkIsIPhoneX()
  15. },
  16.    
  17. checkIsIPhoneX: function () {
  18. const self = this
  19. wx.getSystemInfo({
  20. success: function (res) {
  21. //Judge based on the model
  22. if (res.model.search( 'iPhone X' ) != -1) {
  23. self.globalData.isIPX = true  
  24. }
  25. // Or judge according to screenHeight
  26. // if (res.screenHeight == 812) {
  27. // self.globalData.isIPX = true  
  28. // }
  29. }
  30. })
  31. },
  32. }

If you need to obtain device-related information immediately when the applet is started, you can also call the wx.getSystemInfoSync() method, which will synchronously obtain data and return immediately.

Page adaptation practice

In the development of mini program pages, the main areas that need to be adapted to iPhone X are: Navigation Bar, Tab Bar, and the bottom-sucking button at the bottom of the page.

Navigation bar and tab bar adaptation

If we use the official WeChat Mini Program components for development without customization, set the tabBar page in the app.json file, and set the window navigationStyle value to default, then we do not need to adapt the navigation bar and tab bar on iPhone X, WeChat will automatically adapt them for us. The following figure shows the homepage of the Knowledge Collection Mini Program:

However, if we customize the navigation bar (set the window navigationStyle to custom in the app.json file, only keep the capsule button in the upper right corner, and the developer needs to draw the navigation bar style by himself) and the tab bar, we need to determine the device type on each page, lay out the layout within the safe area for the iPhone X screen, and modify the height value of the relevant bar (see the table above).

Taking custom navigation bar adaptation as an example, the steps are as follows:

(1) In page.js of each page, first read the isIPX value in app.js, as follows:

  1. const app = getApp()
  2.  
  3. Page({
  4. data: {
  5. // Other data on the page...
  6.      
  7. isIPX: app.globalData.isIPX,
  8. },
  9.    
  10. // Other code
  11. }

(2) Then in the page.wxss style file, write two styles for a view, one for the normal screen and the other for the iPhone X screen, as follows:

  1. .navi-bar- view {
  2. height: 64px;
  3. /* Other style values ​​*/
  4. }
  5. .navi-bar -view -IPX {
  6. height: 88px;
  7. /* Other style values ​​*/
  8. }

(3)***In the page.wxml page structure layout, set different class styles for View according to the value of isIPX, as follows:

  1. < view class= "{{isIPX ? 'navi-bar-view-IPX' : 'navi-bar-view'}}" >
  2. </ view >

In addition, for custom navigation bars and tab bars, I recommend following the design specifications of the iPhone UI. The styles can refer to Apple's official renderings:

Adaptation of bottom button

In the mini program page, bottom-sucking buttons are a very common design. We usually put some important buttons at the bottom of the page and let them float. For example, the "Collect" and "Forward" buttons at the bottom of the "Small Collection Details Page" of our Knowledge Collection mini program:

In iPhone X, we need to offset the bottom button 34 pixels upwards. This can be achieved by setting padding-bottom to 34px in the CSS style. The reference code is as follows:

  1. .feed-bottom- view {
  2. width: 100%;
  3. height: 48px; /* Height of the bottom button */
  4. bottom: 0;
  5. opacity: 0.95;
  6. position: fixed;
  7. border- top -style: solid;
  8. border- top -width: 0.5px; /* Height of dividing line */
  9. border-color: lightgrey;
  10. background-color: #F8F8F8;
  11. }
  12. .feed-bottom- view -IPX {
  13. /* iPhone X content is offset 34px up */
  14. padding-bottom: 34px;
  15. }
  16.  
  17. < view class= "{{isIPX ? 'feed-bottom-view feed-bottom-view-IPX' : 'feed-bottom-view'}}" >
  18. <! -- Bottom suction by area -->  
  19. </ view >

Note: As mentioned above, the actual pixel value represented by 1rpx is different for different device widths. In iPhone devices of different sizes (3.5/4.0/4.7/5.5 inches), although their widths are different, the height of the navigation bar + status bar is 64pt (88pt for iPhone X), and the height of the tab bar is 49pt (83pt for iPhone X). Therefore, in mini-program development, when we need to customize the navigation bar, tab bar, or adapt to the top and bottom safe areas of iPhone X, I suggest that the unit here is directly px (corresponding to pt in iOS development in mini-programs) instead of rpx (of course, it is recommended to use rpx for the size description of other elements on the page) to ensure that the height of the final rendering display is consistent with the default iOS system.

Summarize

This article briefly introduces how to adapt to the iPhone X screen in mini-program development. For more details, please refer to the code of the knowledge collection mini-program open sourced on GitHub: awesome-tips-wx-app

<<:  About the arrangement of reminder methods in APP

>>:  Those who desperately promote 5G mobile phones are either stupid or evil.

Recommend

iOS 12.1.1 is officially released: Do you want to try the new features?

Apple released the official version of iOS 12.1.1...

How to effectively improve the conversion effect of information flow?

The cost remained at 180 a few days ago, why did ...

How can Weibo advertising take advantage of the epidemic to revive?

“If you were given a room with a suitable tempera...

The mystery of the silent "sixth sense"

Leviathan Press: Unlike a normal person who is lo...

Analysis of Pinduoduo’s group purchasing model!

The essence of group buying is that multiple user...

Bringing old speakers to life: Chromecast Audio hands-on experience

Along with the release of the upgraded Chromecast...

The most effective App promotion channels, a summary of strengths!

What are the most effective App promotion channel...