Save 37% off PRO during our Black Friday Sale! »

StatusBarをできるだけ簡潔にカスタマイズする

 StatusBarをできるだけ簡潔にカスタマイズする

[Online] potatotips #73 iOS/Android開発Tips共有会のLT資料

https://potatotips.connpass.com/event/202810/
サンプルアプリ: https://github.com/subroh0508/Display-Cutouts-Sample

7c3b3366947123ba6772698b09edf4e2?s=128

subroh_0508

March 26, 2021
Tweet

Transcript

  1. 4UBUVT#BSΛͰ͖Δ͚ͩ ؆ܿʹΧελϚΠζ͢Δ QPUBUPUJQT ʹ͜͠Γ͞ͿΖʙ!TVCSPI@

  2. "CPVU.F ʹ͜͠Γ͞ͿΖʙ ✦౦ژ౎ɾҏ౾େౡग़਎ ✦גࣜձࣾ#&"35"*- ‣ "OESPJEΤϯδχΞ ,PUMJO+BWB ˑ ‣ 8FCΤϯδχΞ

    3BJMT3FBDU
  3. ࿩͢͜ͱ ͜͜ΛΧελϚΠζ͢Δ ࿩Λ͠·͢

  4. 4UBUVT#BSͷΧελϚΠζ 㾎Ͱ͖Δ͜ͱ  എܠͷ৭Λม͑Δ  ΞΠίϯͷ-JHIU%BSLΛ੾Γ׵͑Δ  4UBUVT#BSͷഎޙ·Ͱίϯςϯπ͕ඳը͞ΕΔΑ͏ʹ͢Δ  4UBUVT#BSͦͷ΋ͷΛඇදࣔʹ͢ΔɹɹɹFUD

    BOESPJETUBUVT#BS$PMPSଐੑ BOESPJEXJOEPX-JHIU4UBUVT#BSଐੑ BOESPJEXJOEPX'VMMTDSFFOଐੑ TFU4ZTUFN6J7JTJCJMJUZϝιου ΍ΕΔ͜ͱଟ͗ͯ͢Α͘Θ͔ΒΜ😇 4%,7FSTJPOͰมΘΔͱ͜Ζ΋ͪΐ͍ͪΐ͍͋Δ😇
  5. ࿩͢͜ͱ ˒4UBUVT#BSͷΧελϚΠζʹ͍ͭͯͷ5JQTڞ༗ 㾎4UBUVT#BSഎޙʹ೚ҙͷϝοηʔδΛදࣔ͢Δ 㾎ϝοηʔδͷදࣔɾඇද͕ࣔ੾Γସ͑ΒΕΔ 㾎طଘͷ6*ʹ΋૊ΈࠐΈ΍͘͢ɺ؆ܿͳ࣮૷ 4UBUVT#BSΧελϚΠζͷ ࠷ॳͷҰา͕౿Έग़͠΍͘͢ͳΕ͹ʜ💪

  6. ࣮૷ͷجຊઓུ 㾎ৗʹ4UBUVT#BSͷഎޙ·ͰίϯςϯπΛඳը 㾎5FYU7JFXͷߴ͞Λ4UBUVT#BSͷߴ͞ʹಈతʹ߹ΘͤΔ 㾎4UBUVT#BSͷදࣔɾඇදࣔΛಈతʹ੾Γ׵͑Δ

  7. 4UBUVT#BSͷഎޙ·ͰίϯςϯπΛඳը 㾎TFU4ZTUFN6J7JTJCJMJUZTFU%FDPS'JUT4ZTUFN8JOEPXTϝιου window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 4%, γεςϜ6*ͷදࣔঢ়ଶΛ͍͡ΕΔϝιου /BWJHBUJPO#BSΛӅͨ͠Γ΋Ͱ͖Δ

    window.setDecorFitsSystemWindows(false) 4%, 4%,7FSTJPOҎ߱͸্هίʔυ͚ͩͰ🙆
  8. 5FYU7JFXͷߴ͞Λಈతʹมߋ 㾎TFU0O"QQMZ8JOEPX*OTFUTϝιου  8JOEPX*OTFUT γεςϜ6*ͷඳըྖҬ ΛಈతʹऔಘͰ͖Δ val customStatusBar: TextView =

    ... window.decorView.setOnApplyWindowInsetsListener { _, insets -> setCustomStatusBarHeight(customStatusBar, insets) window.decorView.onApplyWindowInsets(insets) }
  9. 5FYU7JFXͷߴ͞Λಈతʹมߋ 㾎TFU0O"QQMZ8JOEPX*OTFUTϝιου  8JOEPX*OTFUT γεςϜ6*ͷඳըྖҬ ΛಈతʹऔಘͰ͖Δ val customStatusBar: TextView =

    ... window.decorView.setOnApplyWindowInsetsListener { _, insets -> setCustomStatusBarHeight(customStatusBar, insets) window.decorView.onApplyWindowInsets(insets) } γεςϜ6*ͷঢ়ଶ͕มΘΔ౓ʹ8JOEPX*OTFUT͕ྲྀΕͯ͘Δ 8JOEPX*OTFUT͔Β4UBUVT#BSͷߴ͞ΛऔಘͰ͖Δʂ
  10. 5FYU7JFXͷߴ͞Λಈతʹมߋ 㾎TFU0O"QQMZ8JOEPX*OTFUTϝιου  8JOEPX*OTFUT γεςϜ6*ͷඳըྖҬ ΛಈతʹऔಘͰ͖Δ val customStatusBar: TextView =

    ... window.decorView.setOnApplyWindowInsetsListener { _, insets -> setCustomStatusBarHeight(customStatusBar, insets) window.decorView.onApplyWindowInsets(insets) } Կނ͜ͷϝιουͰ8JOEPX*OTFUTΛऔಘͰ͖Δʜʁ ࠷ޙʹ࣮ߦ͍ͯ͠ΔPO"QQMZ8JOEPXT*OTFUTJTԿऀʜʁ
  11. 5FYU7JFXͷߴ͞Λಈతʹมߋ 㾎7JFXEJTQBUDI"QQMZ8JOEPX*OTFUT  ਌7JFX͔Β౉͞Εͨ8JOEPX*OTFUTΛద༻͢Δϝιου public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { try

    { mPrivateFlags3 |= PFLAG3_APPLYING_INSETS; if (mListenerInfo != null && mListenerInfo.mOnApplyWindowInsetsListener != null) { return mListenerInfo.mOnApplyWindowInsetsListener .onApplyWindowInsets(this, insets); } else { return onApplyWindowInsets(insets); } } finally { mPrivateFlags3 &= ~PFLAG3_APPLYING_INSETS; } }
  12. 5FYU7JFXͷߴ͞Λಈతʹมߋ 㾎7JFXEJTQBUDI"QQMZ8JOEPX*OTFUT  ਌7JFX͔Β౉͞Εͨ8JOEPX*OTFUTΛద༻͢Δϝιου public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { try

    { mPrivateFlags3 |= PFLAG3_APPLYING_INSETS; if (mListenerInfo != null && mListenerInfo.mOnApplyWindowInsetsListener != null) { return mListenerInfo.mOnApplyWindowInsetsListener .onApplyWindowInsets(this, insets); } else { return onApplyWindowInsets(insets); } } finally { mPrivateFlags3 &= ~PFLAG3_APPLYING_INSETS; } } TFU0O"QQMZ8JOEPX*OTFUT-JTUFOFSͰ୅ೖͨ͠ -JTUFOFS͕͜͜Ͱݺ͹ΕΔ ˠ8JOEPX*OTFUTͷద༻ॲཧΛ֎෦͔ΒΧελϚΠζͰ͖Δʂ
  13. 5FYU7JFXͷߴ͞Λಈతʹมߋ 㾎7JFXEJTQBUDI"QQMZ8JOEPX*OTFUT  ਌7JFX͔Β౉͞Εͨ8JOEPX*OTFUTΛద༻͢Δϝιου public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { try

    { mPrivateFlags3 |= PFLAG3_APPLYING_INSETS; if (mListenerInfo != null && mListenerInfo.mOnApplyWindowInsetsListener != null) { return mListenerInfo.mOnApplyWindowInsetsListener .onApplyWindowInsets(this, insets); } else { return onApplyWindowInsets(insets); } } finally { mPrivateFlags3 &= ~PFLAG3_APPLYING_INSETS; } } -JTUFOFS͕ଘࡏ͠ͳ͍৔߹ʹ࣮ߦ͞ΕΔͷ͕PO"QQMZ8JOEPX*OTFUT ˠ%FDPS7JFXຊདྷͷద༻ॲཧ΋ඞཁͳͷͰ -JTUFOFSͷ຤ඌͰPO"QQMZ8JOEPX*OTFUT΋ݺͿ
  14. 5FYU7JFXͷߴ͞Λಈతʹมߋ 㾎8JOEPX*OTFUT͔Βͷ4UBUVT#BSͷߴ͞औಘ fun setCustomStatusBarHeight(textView: TextView, insets: WindowInsets) { textView.height =

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) insets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()).top else insets.systemWindowInsetTop }
  15. 5FYU7JFXͷߴ͞Λಈతʹมߋ 㾎8JOEPX*OTFUT͔Βͷ4UBUVT#BSͷߴ͞औಘ fun setCustomStatusBarHeight(textView: TextView, insets: WindowInsets) { textView.height =

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) insets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()).top else insets.systemWindowInsetTop } "1*7FSTJPOҎԼ͸HFU4ZTUFN8JOEPX*OTFU5PQϝιουΛ࢖͏
  16. 5FYU7JFXͷߴ͞Λಈతʹมߋ 㾎8JOEPX*OTFUT͔Βͷ4UBUVT#BSͷߴ͞औಘ fun setCustomStatusBarHeight(textView: TextView, insets: WindowInsets) { textView.height =

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) insets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()).top else insets.systemWindowInsetTop } "1*7FSTJPOҎ্͸HFU*OTFUT*HOPSJOH7JTJCJMJUZϝιουΛ࢖͏ γεςϜ6*ͷදࣔɾඇදࣔͷঢ়ଶ໰ΘͣඳըྖҬΛऔಘ͢Δ ˞HFU*OTFUTϝιουͩͱඇදࣔ࣌ʹۭͷྖҬ͕ฦͬͯ͠·͏
  17. 4UBUVT#BSͷදࣔɾඇදࣔ੾Γସ͑ 㾎TFU4ZTUFN6J7JTJCJMJUZ*OTFUT$POUSPMMFSΛར༻ window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_VISIBLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) 4%,

    GPSTIPX window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) 4%, GPSIJEF
  18. 4UBUVT#BSͷදࣔɾඇදࣔ੾Γସ͑ 㾎TFU4ZTUFN6J7JTJCJMJUZ*OTFUT$POUSPMMFSΛར༻ window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_VISIBLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) 4%,

    GPSTIPX window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) 4%, GPSIJEF 4:45&.@6*@'-"(@7*4*#-&ˠ4UBUVT#BSΛදࣔ
  19. 4UBUVT#BSͷදࣔɾඇදࣔ੾Γସ͑ 㾎TFU4ZTUFN6J7JTJCJMJUZ*OTFUT$POUSPMMFSΛར༻ window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_VISIBLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) 4%,

    GPSTIPX window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) 4%, GPSIJEF 4:45&.@6*@'-"(@'6--4$3&&/ˠ4UBUVT#BSΛඇදࣔ 4:45&.@6*@'-"(@*..&34*7&@45*$,: ˠεϫΠϓͰ൒ಁ໌දࣔ ͠͹Β͘ܦͭͱҾͬࠐΉ
  20. 4UBUVT#BSͷදࣔɾඇදࣔ੾Γସ͑ 㾎TFU4ZTUFN6J7JTJCJMJUZ*OTFUT$POUSPMMFSΛར༻ window.insetsController?.show(WindowInsets.Type.statusBars()) window.insetsController?.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_TOUCH 4%, GPSTIPX window.insetsController?.hide(WindowInsets.Type.statusBars()) window.insetsController?.systemBarsBehavior

    = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE 4%, GPSIJEF
  21. 4UBUVT#BSͷදࣔɾඇදࣔ੾Γସ͑ 㾎TFU4ZTUFN6J7JTJCJMJUZ*OTFUT$POUSPMMFSΛར༻ window.insetsController?.show(WindowInsets.Type.statusBars()) window.insetsController?.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_TOUCH 4%, GPSTIPX window.insetsController?.hide(WindowInsets.Type.statusBars()) window.insetsController?.systemBarsBehavior

    = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE 4%, GPSIJEF "1*7FSTJPOҎ߱͸*OTFUT$POUSPMMFSͷTIPXIJEFͰ 4UBUVT#BSͷදࣔɾඇදࣔΛ੾Γସ͑ΒΕΔʂ
  22. 4UBUVT#BSͷදࣔɾඇදࣔ੾Γସ͑ 㾎TFU4ZTUFN6J7JTJCJMJUZ*OTFUT$POUSPMMFSΛར༻ window.insetsController?.show(WindowInsets.Type.statusBars()) window.insetsController?.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_TOUCH 4%, GPSTIPX window.insetsController?.hide(WindowInsets.Type.statusBars()) window.insetsController?.systemBarsBehavior

    = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE 4%, GPSIJEF #&)&7*03@4)08@#"34@#:@506$)ˠσϑΥϧτ஋ #&)&7*03@4)08@53"/4*&/5@#"34@#:@48*1& ˠεϫΠϓͰ൒ಁ໌දࣔ ͠͹Β͘ܦͭͱҾͬࠐΉ
  23. ࣮૷·ͱΊ 㾎ৗʹ4UBUVT#BSͷഎޙ·ͰίϯςϯπΛඳը 㾎5FYU7JFXͷߴ͞Λ4UBUVT#BSͷߴ͞ʹಈతʹ߹ΘͤΔ 㾎4UBUVT#BSͷදࣔɾඇදࣔΛಈతʹ੾Γ׵͑Δ

  24. ࣮૷·ͱΊ 㾎ৗʹ4UBUVT#BSͷഎޙ·ͰίϯςϯπΛඳը 㾎5FYU7JFXͷߴ͞Λ4UBUVT#BSͷߴ͞ʹಈతʹ߹ΘͤΔ 㾎4UBUVT#BSͷදࣔɾඇදࣔΛಈతʹ੾Γ׵͑Δ ˠTFU4ZTUFN6J7JTJCJMJUZTFU%FDPS'JUT4ZTUFN8JOEPXTΛ ɹ"DUJWJUZPO$SFBUFͰ࣮ߦ͠ɺഎޙ·Ͱίϯςϯπඳը 4UBUVT#BSදࣔͷঢ়ଶʹ͢Δ ˠ%FDPS7JFXTFU0O"QQMZ8JOEPX*OTFUT-JTUFOFSͰ-JTUFOFSΛઃఆ ɹ8JOEPX*OTFUT͔Β4UBUVT#BSͷߴ͞Λऔಘ͠ɺ5FYU7JFXͷߴ͞Λߋ৽͢Δ ˠTFU4ZTUFN6J7JTJCJMJUZ*OTFUT$POUSPMMFSͰ4UBUVT#BSͷදࣔɾඇදࣔΛ੾Γସ͑

  25. 㾎αϯϓϧΞϓϦͰաෆ଍ͳ࣮͘૷ͨ͠ ͸ͣʜ   TVCSPI%JTQMBZ$VUPVUT4BNQMF 㾎ࢀߟจݙ  શը໘Ϟʔυͷ༗ޮԽc"OESPJEσϕϩούʔc"OESPJE%FWFMPQFST EFWFMPQFSBOESPJEDPNUSBJOJOHTZTUFNVJJNNFSTJWF 

    %SPJE,BJHJ4ZTUFN6*Λίϯτϩʔϧͯ͠ɺը໘Λ࠷େݶʹੜ͔ͨ͠ΞϓϦΛߏங͢Δ TQFBLFSEFDLDPNTBUPTIVOCVJMEBQQTUIBUNBLFUIFCFTUVTFTPGEFWJDFTDSFFOTCZDPOUSPMMJOHTZTUFNVJ ৄ͘͠͸ʜ ͋·Γ৮Βͳ͍4UBUVT#BSɺॱংཱͯͯௐ΂Ε͹͜Θ͘ͳ͍ʂ 6*σβΠϯͷ෯͕޿͕Δ͜ͱؒҧ͍ͳ͠ʂੋඇνϟϨϯδʂ💪 5IBOLZPVGPSMJTUFOJOH ϊονରԠ΋ͯ͠·͢ʜ😇