padding top in swift

You can add padding to the top of a UIView in Swift by setting its topAnchor constraint with a constant value. Here's an example:

main.swift
let myView = UIView()

// Add myView to a superview and set constraints to make it fill the superview

// Set padding to the top of myView
myView.topAnchor.constraint(equalTo: superview.topAnchor, constant: 20).isActive = true
227 chars
7 lines

In this example, myView is a newly created UIView. You need to add myView to a superview first and set its constraints to make it fill the superview. Then, you can set its topAnchor constraint with a constant value of 20, which will add a padding of 20 points to the top of myView.

related categories

gistlibby LogSnag