Note — The bridge can also serve images to the app by routing GET requests to an Images folder. However we don’t need to bother with this as our example will download the images instead.
ever-layout-bridge
in your project directory to start the server. It will by default look for a layouts directory ‘Layouts’ on port 3000
, but these options can be configured with arguments: ever-layout-bridge --layouts="MyLayouts” --port=”1234”
import UIKitimport EverLayout@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
#if DEBUGEverLayoutBridge.connectToLayoutServer()#endifreturn true}
applicationDidLoad
.Note — It only really makes sense to connect to the bridge in DEBUG
mode, this isn’t something you want in production.
Double Note — Your HTTP server likely isn’t using an SSL certificate, which means we need to allow arbitrary connections for this to work. If you are unsure on how to do this, see this StackOverflow answer.
connected
message in both the Xcode console, and the terminal window.Now that our app is talking to the server, we want to load a layout in our ViewController. I know we haven’t actually written a layout yet, but that is the whole purpose of the bridge — we can run this blank app and build its layout at runtime!
ViewController.json
file in the Layouts directory.
{"name":"ViewController"}
import UIKit
import EverLayout
class ViewController: UIViewController {
private var layout : EverLayout?
override func viewDidLoad() {
super.viewDidLoad()
let layoutData = NSData(contentsOfFile: Bundle.main.path(forResource: "ViewController", ofType: "json", inDirectory: "Layouts")!)
self.layout = EverLayout(layoutData: layoutData as! Data)
self.layout?.buildLayout(onView: self.view, viewEnvironment: self)
}
}
buildLayout
call we are choosing to build the layout on the controller’s view, but using self
as a view environment. This is because the controller itself will contain properties that we will reference in our layout file.If your app is running and you’ve seen the connected
message, we can run a quick test to see if the layout updates are working.
ViewController.json
to look like the following and hit save.
{"name":"ViewController","root":{"properties":{"backgroundColor":"orange"}}}
This basic layout is applying an orange backgroundColor to its root
, which in our case in the controller’s view. Your app should look like this:
Now, let’s create a real layout:
public let inputUsername : UITextField = UITextField()public let inputPassword : UITextField = UITextField()public let buttonRegister : UIButton = UIButton()public let buttonLogin : UIButton = UIButton()
These properties are required for our login logic, the rest we can build with JSON.
EverLayout has found the text input and button views in the controller and applied a set a properties and layout constraints to them. However we didn’t have to create properties for the logo or even the input labels — they were described in the layout and EverLayout created them on the fly! We’re not likely going to need these properties for any logic, so why should we see them in our controller?
So that’s how EverLayout works! As a note, it’s not good practice to download your app’s logo as you launch, but I just thought it made for a neat demonstration of how powerful this can be.
EverLayout is still a work in progress and very much an experiment, if you’re interesting in learning more about the project you can check out its GitHub, and the Docs.
Thanks for reading!