prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  ios cannot add widgets to main_window.content 
  **Describe the bug**
  A clear and concise description of what the bug is.
  
  **To Reproduce**
  Try to run this app in ios:
  ```
  import toga
  class MyApp(toga.App):
  
      def startup(self):
          """
          Construct and show the Toga application.
  
          Usually, you would add your application to a main content box.
          We then create a main window (with a name matching the app), and
          show the main window.
          """
          self.state = GameState()
          self.main_window = toga.MainWindow(title=self.formal_name)
          box = toga.Box()
          label = toga.Label("Test1")
          box.add(label)
          self.main_window.content = box
          label2 = toga.Label("Test2")
          box.add(label2)
          self.main_window.show()
  MyApp().main_loop()
  ```
  
  **Expected behavior**
  The application should open and shows the labels "Test1" and "Test2". Instead, you get a black screen. In the xcode debugger there is a log message on the "box.add(label2)" line about the label (test2) not having the attribute "viewport".
  
  If you comment out box.add(label2), you correctly get a window that has "Test1".
  
  On targets macos and android, the above test will correctly show "Test1 Test2"
  
  **Environment:**
   - Operating System: macos
   - Python version: 3.8
   - Software versions:
     - Briefcase: 0.3.3
     - Toga: 0.3.0.dev23
     - IOS: iphone 11
  
  This may be related to #225, as you will get a similar error if you try to rebind main_window.content to a different widget.
  ios cannot add widgets to main_window.content 
  **Describe the bug**
  A clear and concise description of what the bug is.
  
  **To Reproduce**
  Try to run this app in ios:
  ```
  import toga
  class MyApp(toga.App):
  
      def startup(self):
          """
          Construct and show the Toga application.
  
          Usually, you would add your application to a main content box.
          We then create a main window (with a name matching the app), and
          show the main window.
          """
          self.state = GameState()
          self.main_window = toga.MainWindow(title=self.formal_name)
          box = toga.Box()
          label = toga.Label("Test1")
          box.add(label)
          self.main_window.content = box
          label2 = toga.Label("Test2")
          box.add(label2)
          self.main_window.show()
  MyApp().main_loop()
  ```
  
  **Expected behavior**
  The application should open and shows the labels "Test1" and "Test2". Instead, you get a black screen. In the xcode debugger there is a log message on the "box.add(label2)" line about the label (test2) not having the attribute "viewport".
  
  If you comment out box.add(label2), you correctly get a window that has "Test1".
  
  On targets macos and android, the above test will correctly show "Test1 Test2"
  
  **Environment:**
   - Operating System: macos
   - Python version: 3.8
   - Software versions:
     - Briefcase: 0.3.3
     - Toga: 0.3.0.dev23
     - IOS: iphone 11
  
  This may be related to #225, as you will get a similar error if you try to rebind main_window.content to a different widget.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/iOS/toga_iOS/widgets/base.py`
  Content:
  ```
  1 from toga_iOS.constraints import Constraints
  2 
  3 
  4 class Widget:
  5     def __init__(self, interface):
  6         self.interface = interface
  7         self.interface._impl = self
  8         self._container = None
  9         self.constraints = None
  10         self.native = None
  11         self.create()
  12         self.interface.style.reapply()
  13 
  14     def set_app(self, app):
  15         pass
  16 
  17     def set_window(self, window):
  18         pass
  19 
  20     @property
  21     def container(self):
  22         return self._container
  23 
  24     @container.setter
  25     def container(self, container):
  26         self._container = container
  27         if self.constraints:
  28             self._container.native.addSubview(self.native)
  29             self.constraints.container = container
  30 
  31         for child in self.interface.children:
  32             child._impl.container = container
  33         self.rehint()
  34 
  35     def set_enabled(self, value):
  36         self.native.enabled = self.interface.enabled
  37 
  38     def focus(self):
  39         self.interface.factory.not_implemented("Widget.focus()")
  40 
  41     # APPLICATOR
  42 
  43     def set_bounds(self, x, y, width, height):
  44         if self.container:
  45             viewport = self.container.viewport
  46         else:
  47             viewport = self.viewport
  48 
  49         self.constraints.update(
  50             x, y + viewport.statusbar_height,
  51             width, height
  52         )
  53 
  54     def set_alignment(self, alignment):
  55         pass
  56 
  57     def set_hidden(self, hidden):
  58         if self._container:
  59             for view in self._container._impl.subviews:
  60                 if view._impl:
  61                     view.setHidden(hidden)
  62 
  63     def set_font(self, font):
  64         # By default, font can't be changed
  65         pass
  66 
  67     def set_color(self, color):
  68         # By default, color can't be changed
  69         pass
  70 
  71     def set_background_color(self, color):
  72         # By default, background color can't be changed
  73         pass
  74 
  75     # INTERFACE
  76 
  77     def add_child(self, child):
  78         if self.container:
  79             child.viewport = self.root.viewport
  80             child.container = self.container
  81 
  82     def add_constraints(self):
  83         self.native.translatesAutoresizingMaskIntoConstraints = False
  84         self.constraints = Constraints(self)
  85 
  86     def rehint(self):
  87         pass
  88
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/src/iOS/toga_iOS/widgets/base.py b/src/iOS/toga_iOS/widgets/base.py
  --- a/src/iOS/toga_iOS/widgets/base.py
  +++ b/src/iOS/toga_iOS/widgets/base.py
  @@ -6,10 +6,12 @@
           self.interface = interface
           self.interface._impl = self
           self._container = None
  +        self._viewport = None
           self.constraints = None
           self.native = None
           self.create()
           self.interface.style.reapply()
  +        self.set_enabled(self.interface.enabled)
   
       def set_app(self, app):
           pass
  @@ -23,15 +25,35 @@
   
       @container.setter
       def container(self, container):
  -        self._container = container
  -        if self.constraints:
  +        if self.container:
  +            if container:
  +                raise RuntimeError('Already have a container')
  +            else:
  +                # existing container should be removed
  +                self.constraints = None
  +                self._container = None
  +                self.native.removeFromSuperview()
  +        elif container:
  +            # setting container
  +            self._container = container
               self._container.native.addSubview(self.native)
  +            if not self.constraints:
  +                self.add_constraints()
               self.constraints.container = container
   
           for child in self.interface.children:
               child._impl.container = container
  +
           self.rehint()
   
  +    @property
  +    def viewport(self):
  +        return self._viewport
  +
  +    @viewport.setter
  +    def viewport(self, viewport):
  +        self._viewport = viewport
  +
       def set_enabled(self, value):
           self.native.enabled = self.interface.enabled
   
  @@ -41,13 +63,13 @@
       # APPLICATOR
   
       def set_bounds(self, x, y, width, height):
  +        offset_y = 0
           if self.container:
  -            viewport = self.container.viewport
  -        else:
  -            viewport = self.viewport
  -
  +            offset_y = self.container.viewport.statusbar_height
  +        elif self.viewport:
  +            offset_y = self.viewport.statusbar_height
           self.constraints.update(
  -            x, y + viewport.statusbar_height,
  +            x, y + offset_y,
               width, height
           )
   
  @@ -55,8 +77,8 @@
           pass
   
       def set_hidden(self, hidden):
  -        if self._container:
  -            for view in self._container._impl.subviews:
  +        if self.container:
  +            for view in self.container._impl.subviews:
                   if view._impl:
                       view.setHidden(hidden)
   
  @@ -75,10 +97,16 @@
       # INTERFACE
   
       def add_child(self, child):
  -        if self.container:
  -            child.viewport = self.root.viewport
  +
  +        if self.viewport:
  +            # we are the the top level UIView
  +            child.container = self
  +        else:
               child.container = self.container
   
  +    def remove_child(self, child):
  +        child.container = None
  +
       def add_constraints(self):
           self.native.translatesAutoresizingMaskIntoConstraints = False
           self.constraints = Constraints(self)
  
verification_info: {"golden_diff": "diff --git a/src/iOS/toga_iOS/widgets/base.py b/src/iOS/toga_iOS/widgets/base.py\n--- a/src/iOS/toga_iOS/widgets/base.py\n+++ b/src/iOS/toga_iOS/widgets/base.py\n@@ -6,10 +6,12 @@\n         self.interface = interface\n         self.interface._impl = self\n         self._container = None\n+        self._viewport = None\n         self.constraints = None\n         self.native = None\n         self.create()\n         self.interface.style.reapply()\n+        self.set_enabled(self.interface.enabled)\n \n     def set_app(self, app):\n         pass\n@@ -23,15 +25,35 @@\n \n     @container.setter\n     def container(self, container):\n-        self._container = container\n-        if self.constraints:\n+        if self.container:\n+            if container:\n+                raise RuntimeError('Already have a container')\n+            else:\n+                # existing container should be removed\n+                self.constraints = None\n+                self._container = None\n+                self.native.removeFromSuperview()\n+        elif container:\n+            # setting container\n+            self._container = container\n             self._container.native.addSubview(self.native)\n+            if not self.constraints:\n+                self.add_constraints()\n             self.constraints.container = container\n \n         for child in self.interface.children:\n             child._impl.container = container\n+\n         self.rehint()\n \n+    @property\n+    def viewport(self):\n+        return self._viewport\n+\n+    @viewport.setter\n+    def viewport(self, viewport):\n+        self._viewport = viewport\n+\n     def set_enabled(self, value):\n         self.native.enabled = self.interface.enabled\n \n@@ -41,13 +63,13 @@\n     # APPLICATOR\n \n     def set_bounds(self, x, y, width, height):\n+        offset_y = 0\n         if self.container:\n-            viewport = self.container.viewport\n-        else:\n-            viewport = self.viewport\n-\n+            offset_y = self.container.viewport.statusbar_height\n+        elif self.viewport:\n+            offset_y = self.viewport.statusbar_height\n         self.constraints.update(\n-            x, y + viewport.statusbar_height,\n+            x, y + offset_y,\n             width, height\n         )\n \n@@ -55,8 +77,8 @@\n         pass\n \n     def set_hidden(self, hidden):\n-        if self._container:\n-            for view in self._container._impl.subviews:\n+        if self.container:\n+            for view in self.container._impl.subviews:\n                 if view._impl:\n                     view.setHidden(hidden)\n \n@@ -75,10 +97,16 @@\n     # INTERFACE\n \n     def add_child(self, child):\n-        if self.container:\n-            child.viewport = self.root.viewport\n+\n+        if self.viewport:\n+            # we are the the top level UIView\n+            child.container = self\n+        else:\n             child.container = self.container\n \n+    def remove_child(self, child):\n+        child.container = None\n+\n     def add_constraints(self):\n         self.native.translatesAutoresizingMaskIntoConstraints = False\n         self.constraints = Constraints(self)\n", "issue": "ios cannot add widgets to main_window.content \n**Describe the bug**\r\nA clear and concise description of what the bug is.\r\n\r\n**To Reproduce**\r\nTry to run this app in ios:\r\n```\r\nimport toga\r\nclass MyApp(toga.App):\r\n\r\n    def startup(self):\r\n        \"\"\"\r\n        Construct and show the Toga application.\r\n\r\n        Usually, you would add your application to a main content box.\r\n        We then create a main window (with a name matching the app), and\r\n        show the main window.\r\n        \"\"\"\r\n        self.state = GameState()\r\n        self.main_window = toga.MainWindow(title=self.formal_name)\r\n        box = toga.Box()\r\n        label = toga.Label(\"Test1\")\r\n        box.add(label)\r\n        self.main_window.content = box\r\n        label2 = toga.Label(\"Test2\")\r\n        box.add(label2)\r\n        self.main_window.show()\r\nMyApp().main_loop()\r\n```\r\n\r\n**Expected behavior**\r\nThe application should open and shows the labels \"Test1\" and \"Test2\". Instead, you get a black screen. In the xcode debugger there is a log message on the \"box.add(label2)\" line about the label (test2) not having the attribute \"viewport\".\r\n\r\nIf you comment out box.add(label2), you correctly get a window that has \"Test1\".\r\n\r\nOn targets macos and android, the above test will correctly show \"Test1 Test2\"\r\n\r\n**Environment:**\r\n - Operating System: macos\r\n - Python version: 3.8\r\n - Software versions:\r\n   - Briefcase: 0.3.3\r\n   - Toga: 0.3.0.dev23\r\n   - IOS: iphone 11\r\n\r\nThis may be related to #225, as you will get a similar error if you try to rebind main_window.content to a different widget.\nios cannot add widgets to main_window.content \n**Describe the bug**\r\nA clear and concise description of what the bug is.\r\n\r\n**To Reproduce**\r\nTry to run this app in ios:\r\n```\r\nimport toga\r\nclass MyApp(toga.App):\r\n\r\n    def startup(self):\r\n        \"\"\"\r\n        Construct and show the Toga application.\r\n\r\n        Usually, you would add your application to a main content box.\r\n        We then create a main window (with a name matching the app), and\r\n        show the main window.\r\n        \"\"\"\r\n        self.state = GameState()\r\n        self.main_window = toga.MainWindow(title=self.formal_name)\r\n        box = toga.Box()\r\n        label = toga.Label(\"Test1\")\r\n        box.add(label)\r\n        self.main_window.content = box\r\n        label2 = toga.Label(\"Test2\")\r\n        box.add(label2)\r\n        self.main_window.show()\r\nMyApp().main_loop()\r\n```\r\n\r\n**Expected behavior**\r\nThe application should open and shows the labels \"Test1\" and \"Test2\". Instead, you get a black screen. In the xcode debugger there is a log message on the \"box.add(label2)\" line about the label (test2) not having the attribute \"viewport\".\r\n\r\nIf you comment out box.add(label2), you correctly get a window that has \"Test1\".\r\n\r\nOn targets macos and android, the above test will correctly show \"Test1 Test2\"\r\n\r\n**Environment:**\r\n - Operating System: macos\r\n - Python version: 3.8\r\n - Software versions:\r\n   - Briefcase: 0.3.3\r\n   - Toga: 0.3.0.dev23\r\n   - IOS: iphone 11\r\n\r\nThis may be related to #225, as you will get a similar error if you try to rebind main_window.content to a different widget.\n", "before_files": [{"content": "from toga_iOS.constraints import Constraints\n\n\nclass Widget:\n    def __init__(self, interface):\n        self.interface = interface\n        self.interface._impl = self\n        self._container = None\n        self.constraints = None\n        self.native = None\n        self.create()\n        self.interface.style.reapply()\n\n    def set_app(self, app):\n        pass\n\n    def set_window(self, window):\n        pass\n\n    @property\n    def container(self):\n        return self._container\n\n    @container.setter\n    def container(self, container):\n        self._container = container\n        if self.constraints:\n            self._container.native.addSubview(self.native)\n            self.constraints.container = container\n\n        for child in self.interface.children:\n            child._impl.container = container\n        self.rehint()\n\n    def set_enabled(self, value):\n        self.native.enabled = self.interface.enabled\n\n    def focus(self):\n        self.interface.factory.not_implemented(\"Widget.focus()\")\n\n    # APPLICATOR\n\n    def set_bounds(self, x, y, width, height):\n        if self.container:\n            viewport = self.container.viewport\n        else:\n            viewport = self.viewport\n\n        self.constraints.update(\n            x, y + viewport.statusbar_height,\n            width, height\n        )\n\n    def set_alignment(self, alignment):\n        pass\n\n    def set_hidden(self, hidden):\n        if self._container:\n            for view in self._container._impl.subviews:\n                if view._impl:\n                    view.setHidden(hidden)\n\n    def set_font(self, font):\n        # By default, font can't be changed\n        pass\n\n    def set_color(self, color):\n        # By default, color can't be changed\n        pass\n\n    def set_background_color(self, color):\n        # By default, background color can't be changed\n        pass\n\n    # INTERFACE\n\n    def add_child(self, child):\n        if self.container:\n            child.viewport = self.root.viewport\n            child.container = self.container\n\n    def add_constraints(self):\n        self.native.translatesAutoresizingMaskIntoConstraints = False\n        self.constraints = Constraints(self)\n\n    def rehint(self):\n        pass\n", "path": "src/iOS/toga_iOS/widgets/base.py"}], "after_files": [{"content": "from toga_iOS.constraints import Constraints\n\n\nclass Widget:\n    def __init__(self, interface):\n        self.interface = interface\n        self.interface._impl = self\n        self._container = None\n        self._viewport = None\n        self.constraints = None\n        self.native = None\n        self.create()\n        self.interface.style.reapply()\n        self.set_enabled(self.interface.enabled)\n\n    def set_app(self, app):\n        pass\n\n    def set_window(self, window):\n        pass\n\n    @property\n    def container(self):\n        return self._container\n\n    @container.setter\n    def container(self, container):\n        if self.container:\n            if container:\n                raise RuntimeError('Already have a container')\n            else:\n                # existing container should be removed\n                self.constraints = None\n                self._container = None\n                self.native.removeFromSuperview()\n        elif container:\n            # setting container\n            self._container = container\n            self._container.native.addSubview(self.native)\n            if not self.constraints:\n                self.add_constraints()\n            self.constraints.container = container\n\n        for child in self.interface.children:\n            child._impl.container = container\n\n        self.rehint()\n\n    @property\n    def viewport(self):\n        return self._viewport\n\n    @viewport.setter\n    def viewport(self, viewport):\n        self._viewport = viewport\n\n    def set_enabled(self, value):\n        self.native.enabled = self.interface.enabled\n\n    def focus(self):\n        self.interface.factory.not_implemented(\"Widget.focus()\")\n\n    # APPLICATOR\n\n    def set_bounds(self, x, y, width, height):\n        offset_y = 0\n        if self.container:\n            offset_y = self.container.viewport.statusbar_height\n        elif self.viewport:\n            offset_y = self.viewport.statusbar_height\n        self.constraints.update(\n            x, y + offset_y,\n            width, height\n        )\n\n    def set_alignment(self, alignment):\n        pass\n\n    def set_hidden(self, hidden):\n        if self.container:\n            for view in self.container._impl.subviews:\n                if view._impl:\n                    view.setHidden(hidden)\n\n    def set_font(self, font):\n        # By default, font can't be changed\n        pass\n\n    def set_color(self, color):\n        # By default, color can't be changed\n        pass\n\n    def set_background_color(self, color):\n        # By default, background color can't be changed\n        pass\n\n    # INTERFACE\n\n    def add_child(self, child):\n\n        if self.viewport:\n            # we are the the top level UIView\n            child.container = self\n        else:\n            child.container = self.container\n\n    def remove_child(self, child):\n        child.container = None\n\n    def add_constraints(self):\n        self.native.translatesAutoresizingMaskIntoConstraints = False\n        self.constraints = Constraints(self)\n\n    def rehint(self):\n        pass\n", "path": "src/iOS/toga_iOS/widgets/base.py"}]}