Search code examples
swiftalignmenttext-alignment

Swift: How many types of method available for alignment UILabel and other widget in swift?


Hello i am flutter developer and new to swift. could you please help me to figure out which method is perfect or in which situation which method should I user for alignment widgets in swift?

Like : UILabel, Image,Padding , Row,COlumn , have two tips of method see below.

//Method - 1
scoreLabel.frame = CGRect( x: 50, y: 50, width: 100, height: 50)
//method - 2 
NSLayoutConstraint.activate([scoreLabel.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
                                     scoreLabel.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),])
 

So now is other any other method also for alignment or which would be used in live project or right way for every devices.


Solution

  • Those are different approaches in laying out UI elements in UIKit. The first one is manually setting frames for your views, the other one is called AutoLayout. There're plenty of discussions comparing their differences.

    Apart from UIKit, there's also another way of building UI for Apple platforms, which becoming more popular – SwiftUI.