Thursday, March 3, 2016

Swift Building Basic UI

The Apple reference gives a Food tracker app building UI. 

the basic UI had one label, one text filed and a button. The tutorial began by dragging and dropping the components to the view controller and then it talks about adopting the auto layout. 

Few items to note are: 

1. Adding spacing between components in stack view. This can be done by Selecting Stack view in the outline view and on the attribute inspector, in the spacing field, enter the value desired. 
2. Now we can align the Stack view w.r.to the parent container and for this need to use the Pin menu. We can select the vertical or horizontal constraints here. 
3. Having the #2 done, the Text field in the stack view was not stretching. This is because the default size is intrinsic size. This can also be Pinned after selecting the horizontal constraints according to the spacing desired. Also, can change the Intrinsic size to place holder Intrinsic content size refers to the minimum size needed to display all the content in the view. You can assign UI elements a placeholder intrinsic content size if you need to design a UI for a different size than you can anticipate at design time. Right now, the text field’s only content is its placeholder string, but the actual text a user enters could be longer than that.

Having the 3 steps above done, the UI will be something like this. 
Next part was connecting the built UI to the code. 

In Swift, instead of using #pragma, we need to use //MARK:
The IBoutlet can be created by just dragging the component to the code area in assistant editor. when we do this for the textfield, we can notice that the IBoutlet get created with an implicitly unwrapped optional. (optional that is always having value after it first set) 


For adding actions, again drag and drop to the function area. An area can be made as method area by declaring //MARK: Actions. Now once drag and drop the button, the popup appears where we can fill in the attributes such as UIButton, etc. 
In Swift, AnyObject is a type used to describe an object that can belong to any class. Specifying the type of this action method to be UIButton means that only button objects can connect to this action. Although this isn’t significant for the action you’re creating right now, it’s important to remember for later.

Implementing the protocols is easy, in Objective C, we had to use and in the Swift, we can do this by just writing with a comma separated list. 

References:

No comments:

Post a Comment