How do I make ur react native app stable on different screen size
My react native app on my emulator is okay but when I test it on a smaller screen size the soon doesn't fit the screen
Hi. You are right, I don't know how I can forgot to talk about responsiveness. I will update that soon on my repository. Here are what I do to handle that:
Avoid as much as I can "absolute" position, hard values (100, 300, 1680,...) especially for big ones.
Instead, I use flex, and % values
I have this normalize function for adapting hard values accordingly to the screen's width or height. I might upload it on the repository later:
import{Dimensions,Platform,PixelRatio}from'react-native';exportconst{width:SCREEN_WIDTH,height:SCREEN_HEIGHT}=Dimensions.get('window',);// based on iphone X's scaleconstwscale=SCREEN_WIDTH/375;consthscale=SCREEN_HEIGHT/812;exportfunctionnormalize(size,based='width'){constnewSize=based==='height'?size*hscale:size*wscale;if(Platform.OS==='ios'){returnMath.round(PixelRatio.roundToNearestPixel(newSize));}else{returnMath.round(PixelRatio.roundToNearestPixel(newSize))-2;}}
So now I can use:
// iphone Xnormalize(100)// = 100// iphone 5snormalize(100)// = maybe 80// You can choose either "width" (default) or "height" depend on cases:container={width:normalize(100,"width"),// "width" is optional, it's defaultheight:normalize(100,"height")}
Before pushing, I test my work on 3 differents emulators: iphone5s (small), iphone 8 (medium) and iphone Xs Max (big)
Just wanted to confirm, do we need the Math.round operation in normalize function? Since roundToNearestPixel returns a layout dimension and guarentees that it will be rendered to a non-fractional amount of pixels in the native side, I think we don't need to round once more. Also, what is the reason for 2dip difference in Android?
Well the function adapt a hard value like 13 depending on the screen sizes for exemple on the iPhone X the fontSize will be 13 but on the iPhone5, the fontSize is 11
How do I make ur react native app stable on different screen size
My react native app on my emulator is okay but when I test it on a smaller screen size the soon doesn't fit the screen
Hi. You are right, I don't know how I can forgot to talk about responsiveness. I will update that soon on my repository. Here are what I do to handle that:
normalizefunction for adapting hard values accordingly to the screen's width or height. I might upload it on the repository later:So now I can use:
Hope this help :D
Hi Arthur, thanks for the article.
Just wanted to confirm, do we need the Math.round operation in normalize function? Since roundToNearestPixel returns a layout dimension and guarentees that it will be rendered to a non-fractional amount of pixels in the native side, I think we don't need to round once more. Also, what is the reason for 2dip difference in Android?
Thanks a lot.
I'd like to also ask as to how do you deal with system font sizes? Or is that something that should be considered while designing the app beforehand?
I use the function normalize() that I mentionned in the post so I don't have much problems with the font sizes.
Oh, so that's not just for the actual screen size of the phone, but also for font sizes? TIL
Well the function adapt a hard value like 13 depending on the screen sizes for exemple on the iPhone X the fontSize will be 13 but on the iPhone5, the fontSize is 11
I am getting a 404 on the GitHub link
Ah yes I forgot to update it. Thank you. Here is the link github.com/NewBieBR/typescript-rea...
thanks :)
Excellent post, trying to use TS on my next project too!