Ported version of YogaKit.
Dependency on yoga.
package respository URL: https://github.com/cntrump/SwiftYogaKit.git
github "cntrump/SwiftYogaKit" ~> 1.0.0
import SwiftYogaKit
iOS / tvOS
func applicationDidFinishLaunching(_ application: UIApplication) {
UIView.SwiftYogaKitSwizzle()
}
macOS
func applicationWillFinishLaunching(_ aNotification: Notification) {
NSView.SwiftYogaKitSwizzle()
}
override func viewDidLoad() {
let root = self.view!
root.backgroundColor = .white
root.yoga
.alignItems(.center)
.justifyContent(.center)
let child1 = UIView()
child1.backgroundColor = .blue
child1.yoga
.width(100)
.height(10)
.marginBottom(25)
root.addSubview(child1)
let child2 = UIView()
child2.backgroundColor = .green
child2.yoga
.alignSelf(.flexEnd)
.width(200)
.height(200)
root.addSubview(child2)
let child3 = UIView()
child3.backgroundColor = .yellow
child3.yoga
.alignSelf(.flexStart)
.width(100)
.height(100)
root.addSubview(child3)
}
tableView.estimatedRowHeight = UITableView.automaticDimension
tableView.rowHeight = UITableView.automaticDimension
Subclass UITableViewCell
, calculate cell size in sizeThatFits(_:)
override func sizeThatFits(_ size: CGSize) -> CGSize {
var w: CGFloat = 0
if #available(iOS 11.0, *) {
w = size.width - self.safeAreaInsets.left - self.safeAreaInsets.right
}
return contentView.yoga.calculateLayout(size: CGSize(width: w, height: .nan))
}