![]() It will be automatically imported into your playground page as a module so you need to make sure the code there has a public API to be visible to the code in the page. This code will be compiled less frequently and run more efficiently. If your single playground page gets so long it takes forever to compile with every change, then you can move finished code into the Sources/ folder. You can also move helper code into Sources/. If you don’t want your playground automatically recompiling and re-running after every edit, which is the default, you should use the dropdown at the bottom of the edit window to switch the playground from Automatically Run to Manually Run. So you may need to manually invoke layout or drawing, for instance by invoking setNeedsLayout() and layoutIfNeeded() at the end of your script. But a playground runs once, like a script. In an app, UIKit creates a run loop, which is always in the background, repeatedly running your layout and drawing code when needed. Oddly, this will be the directory ~/Documents/Shared Playground Data and is shared across all playgrounds. You can also write to the filesystem by building a path based on the directory provided by playgroundSharedDataDirectory. You can include read-only assets like images by putting them in your playground’s Resources/ folder, then access them with the usual Foundation Bundle API. Then make sure the dropdown in the jump bar is set so that the assistant editor shows a Live View, not a generated interface or something else. CMD-OPT- will do this, or you can use the segmented control in the toolbar. To actually see the view in Xcode you need to reveal the Assistant Editor. To display a view or a view controller, assign it to. Here are the key points to remember when using playgrounds for UI development:Īt the top of your playground page, you should import PlaygroundSupport. The example playgrounds below show most of these, so hopefully they are a good starting point for your own exploration. (Eric Sadun’s advice is excellent, so her simfix shortcut is certainly worth a try.)īeyond that, the main downside is that developing UI in a playground requires a few tricks that are fairly simple but obscure and therefore easy to forget. In those cases, you have to quit your playground and restart. Sometimes, even if you do everything right, they will crash or hang. That said, every rose has its thorns, and even your neighborhood playground may have a few inexplicable empty beer cans lying around by the preschool swing set.įirst, full disclosure: Xcode playgrounds and Swift Playgrounds on the iPad are a bit unstable. ![]() In fact, this more modular design is preferable. If you also need the view to have an API that does rely on the app’s custom types, you can layer that on afterwards with an extension. But oftentimes this is not necessary and you’d be better off to develop your custom view in the way Apple would, with a clean external API that relies only on standard basic types like String and UIImage. Or you might place key logic in the view controller expected to use the view. This final point is an important one: you might build a view to be configured by your app’s model type, like your User type. If you develop your view within your app, you will be tempted to think of the view as an outgrowth of your app, and to build it in such a way that it depends on your app. Seeing and working on a single view in isolation help you focus on the problem at hand rather than get distracted by the rest of the app.Įnforced modularity. Developing a view in the context of an app is distracting. So playgrounds can be faster, which keeps you in a state of flow.įocus. Although you could develop the view by repeatedly doing Build & Run on your app, you’d waste a lot of time compiling and then navigating through the app to the new view. This is often the case when you’re developing a moderately complex custom UIView subclass. More exactly, they’re great when you want rapid, iterative development of a well-isolated module. But since it seems like it’s not a common practice, I thought it might be interesting to share a few notes and examples of how we have used playgrounds developing the Topology app. ![]() There are WWDC sessions that go through the ins and outs of playgrounds. The talk was at NSMeetup, the local San Francisco meetup for iOS development, and someone in the audience asked if you can use Auto Layout in Xcode playgrounds.Īnswer: yes, you can! And playgrounds are great for UI development – flawed, but great. Last week, Agnes gave a talk on how we use–and love!–Auto Layout here at Topology. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |