omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Safe area

    Pythonista
    1
    1
    985
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mikael
      mikael last edited by

      Here’s a utility view that can be presented as a root view that stays within the device safe area. Alternatively, you can init it with another view to be used as a superview.

      import objc_util
      import ui
      
      NSLayoutConstraint = objc_util.ObjCClass('NSLayoutConstraint')
      
      
      class SafeAreaView(ui.View):
          
          def __init__(self, superview=None, **kwargs):
              super().__init__(**kwargs)
              
              if superview:
                  self._set_constraints(superview)
                  
          def _set_constraints(self, superview):
              superview.add_subview(self)
              selfo = self.objc_instance
              supero = superview.objc_instance
              selfo.setTranslatesAutoresizingMaskIntoConstraints_(False)
              safe = supero.safeAreaLayoutGuide()
              NSLayoutConstraint.activateConstraints_([
                  selfo.topAnchor().constraintEqualToAnchor_constant_(
                      safe.topAnchor(), 0),
                  selfo.bottomAnchor().constraintEqualToAnchor_constant_(
                      safe.bottomAnchor(), 0),
                  selfo.leftAnchor().constraintEqualToAnchor_constant_(
                      safe.leftAnchor(), 0),
                  selfo.rightAnchor().constraintEqualToAnchor_constant_(
                      safe.rightAnchor(), 0),
              ])
              
          def present(self, *args, **kwargs):
              real_root = ui.View(background_color=self.background_color)
              self._set_constraints(real_root)
              real_root.present(*args, **kwargs)
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post
      Powered by NodeBB Forums | Contributors