Custom controls using @IBDesignable and @IBInsepectable

You can design custom controls and render them in real time from Interface Builder using @IBDesignable and @IBInspectable attributes.

In the following example we will create a custom UIButton and change its corner radius, border color and border width from InterFace Builder with the help of @IBDesignable and @IBInspectable attributes. Continue reading “Custom controls using @IBDesignable and @IBInsepectable”

How to change UILabel height dynamically to fit all text

In two steps we can make a UILabel change its height dynamically without writing code.

UILabel dynamic height

First select your UILabel and from “Identity inspector” set number of lines property to 0.

UILabel dynamic height

Next add Top, Trailing, Leading and Height constraints then select the height constraint and change its “Relation” property from “Equal” to “Greater Than or Equal” Continue reading “How to change UILabel height dynamically to fit all text”

Remove empty rows at the end of UITableView

Removing Empty Rows from the end of UITableView

Sometimes you have a table view with very few number of rows that are not covering the whole screen height.

In that case Xcode will add extra empty rows to your table view which looks very unprofessional. Continue reading “Remove empty rows at the end of UITableView”

Custom header view from xib for UITableView

In this step by step tutorial we will create a custom header view to use in UITableView and it will be loaded from xib file.

Creating the UITableViewHeaderFooterView class and xib

1- First of all create a new Swift file and make it subclass of UITableViewHeaderFooterView and name it “DemoHeaderView” Continue reading “Custom header view from xib for UITableView”

Passing data back and forward between view controllers

I will go straight to the point and tell you how to send data back and forward in different situations.

This code was written in Swift 3

Sending data forward using Segue

If you are navigating from ViewControllerA to ViewControllerB using Segue called “toViewControllerB” then do the following steps Continue reading “Passing data back and forward between view controllers”

Working with CALayer

What is CALayer

CALayer is an object responsible for drawing and animations and it is part of Core Animation framework.

It is always used as backing store for UIViews to manage visual content but it can be also used without views.

If it was created by a view the view assigns itself as delegate for that layer and you should not change that. Continue reading “Working with CALayer”

UIViewController lifecycle explained

UIViewController Lifecycle

In this tutorial we will explain the lifecycle of UIViewController in iOS, these methods will run in the following order:

1- init?(coder aDecoder: NSCoder)

If you are using storyboards this is where the controller is initialized, it is called one time only during the view controller lifetime.

2- loadView()

Creates the view that the controller manage, the view controller calls this method when its view property is requested but is currently nil.

This method loads or creates a view and assign it to the view property. Continue reading “UIViewController lifecycle explained”

How to change UITextField placeholder color in Swift 3 and Interface Builder

You can change UITextField placeholder color programmatically or using interface builder

Changing UITextField placeholder color in Swift 3

1- Create a new UITextField subclass and  override its

layoutSubviews() and insert in it this line of code

self.setValue(UIColor.init(red: 178/255.0, green: 34/255.0, blue: 78/255.0, alpha: 0.5), forKeyPath: "_placeholderLabel.textColor")

Continue reading “How to change UITextField placeholder color in Swift 3 and Interface Builder”

How to add underline to UITextField in Swift 3

UITextField Underlined In Swift 3

Create a custom UITexField and add this method to it then override

layoutSubviews() and call it from there

    func underlined(){
        let border = CALayer()
        let lineWidth = CGFloat(0.3)
        border.borderColor = UIColor.lightGray.cgColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - lineWidth, width:  self.frame.size.width, height: self.frame.size.height)
        border.borderWidth = lineWidth
        self.layer.addSublayer(border)
        self.layer.masksToBounds = true
    }

Good to know How to change UITextField placeholder color

Custom UITableViewCell from xib file in Swift 3

Create the custom UITableViewCell from a xib file

In this tutorial we will create a custom cell from xib file and learn how to use it in UITableView using Swift 3

Create Xcode project

1- Create a new Xcode project and select “Single View Application” template and click next

2- Type you project name, in my case i named it “CellExample” and select Swift from Language list and iPhone from Devices list and click next

custom uitableviewcell from xib in swift

3- select a location for your project then click finish.

Create UITableViewCell class and xib

4-Now create a new file and select “Cocoa Touch Class” and press next Continue reading “Custom UITableViewCell from xib file in Swift 3”