I have an app which uses Stack Views to update its UI depending on the number of pigs. For example below, 1 pig and 4 pigs.
I set up Stack Views in Storyboard and set isHidden to true based on how many pigs there are. I set up my custom view with my class below and set up x and y values of labels based on the current frame.
class PiggyBankView: UIView {
let titleLbl = UILabel()
let imageView = UIImageView()
let budgetLbl = CountingLabel()
let padding : CGFloat = 10.0
var parentFrame : CGRect?
var mainVC : MainVC?
init(parent : CGRect, frame : CGRect) {
self.parentFrame = parent
super.init(frame : frame)
self.addCustomView()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.addCustomView()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addCustomView() {
self.backgroundColor = UIColor.clear
//Set Padding For Image View
let pad = padding * 2
let innerSize = CGSize(width: self.frame.size.width - pad, height: self.frame.size.height - pad)
let innerFrame = CGRect(x: padding, y: padding, width: innerSize.width, height: innerSize.height)
//Image View
imageView.frame = innerFrame
imageView.backgroundColor = UIColor.clear
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(named: "pig-main")
let centerPoint = CGPoint(x: imageView.frame.size.width / 2, y: imageView.frame.size.height / 2)
// Title Label and Budget Label Based on its frame
let size = CGSize(width: self.frame.size.width - pad, height: self.frame.size.height / 10)
//Test
self.setNeedsLayout()
self.layoutIfNeeded()
print("HEIGHT : \(self.frame.size.height), WIDTH : \(self.frame.size.width)")
var parentHeight : CGFloat = 0.0
var parentWidth : CGFloat = 0.0
if let mainFrame = parentFrame {
print(mainFrame.size.height)
print(mainFrame.size.width)
parentHeight = mainFrame.size.height
parentWidth = mainFrame.size.width
}
let frameWidth = floor(self.frame.size.width)
let frameHeight = floor(self.frame.size.height)
var titleFrame : CGRect
var labelFrame : CGRect
var dragFromFrame : CGRect
print("\(parentWidth / 2)")
//TALL HEIGHT WIDE WIDTH
if frameHeight == parentHeight && frameWidth == parentWidth {
print("1 TALL HEIGHT / WIDE WIDTH ")
titleFrame = CGRect(x: padding, y: (size.height * 2) - 20, width: size.width, height: size.height)
labelFrame = CGRect(x: padding, y: (size.height * 7) + 25, width: size.width, height: size.height)
// TALL HEIGHT SMALL WIDTH
}else if frameHeight == parentHeight && frameWidth == floor(parentWidth / 2) {
print("2 TALL HEIGHT / SMALL WIDTH")
titleFrame = CGRect(x: padding, y: size.height * 3, width: size.width, height: size.height)
labelFrame = CGRect(x: padding, y: size.height * 6 + 15, width: size.width, height: size.height)
// SHORT HEIGHT - WIDE WIDTH
}else if frameHeight == floor(parentHeight / 2) && frameWidth == parentWidth {
print("3 SHORT HEIGHT / WIDE WIDTH Pig")
titleFrame = CGRect(x: padding, y: -10, width: size.width, height: size.height)
labelFrame = CGRect(x: padding, y: size.height * 9 + 10, width: size.width, height: size.height)
// SHORT HEIGHT - SMALL WIDTH
}else {
print("4 SHORT HEIGHT / SHORT WIDTH Pig")
titleFrame = CGRect(x: padding, y: (size.height * 2) - 10, width: size.width, height: size.height)
labelFrame = CGRect(x: padding, y: (size.height * 7) + 15, width: size.width, height: size.height)
}
// Title label
titleLbl.frame = titleFrame
titleLbl.textAlignment = .center
titleLbl.backgroundColor = UIColor.clear
titleLbl.font = UIFont(name: "CaviarDreams", size: 20.0)
titleLbl.textColor = UIColor.black
titleLbl.adjustsFontSizeToFitWidth = true
//Price Label
let budgetFrame = CGRect(x: labelFrame.origin.x, y: labelFrame.origin.y, width: labelFrame.width * 0.8, height: labelFrame.height)
budgetLbl.frame = budgetFrame
budgetLbl.center = CGPoint(x: self.frame.width / 2, y: budgetFrame.origin.y)
budgetLbl.textAlignment = .center
budgetLbl.backgroundColor = UIColor.clear
budgetLbl.font = UIFont(name: "Roboto", size: 16.0)
budgetLbl.textColor = UIColor.black
budgetLbl.adjustsFontSizeToFitWidth = true
self.addSubview(imageView)
self.addSubview(titleLbl)
self.addSubview(budgetLbl)
self.addSubview(coinImageView)
imageView.addSubview(dragFromView)
}
This gets the job done but I feel like my code is messy and unnecessary. Is there a better way to do this?