Skip to content

Instantly share code, notes, and snippets.

@alonecuzzo
Created December 7, 2015 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alonecuzzo/d5905f830bcdc91fbefd to your computer and use it in GitHub Desktop.
Save alonecuzzo/d5905f830bcdc91fbefd to your computer and use it in GitHub Desktop.
class CustomTableHeaderView: UIView {
//MARK: Property
let label = UILabel(frame: CGRectZero)
let backbutton = UIButton(frame: CGRectZero)
//MARK: Method
override init(frame: CGRect) {
super.init(frame: frame)
label.text = "Add custom location"
label.textAlignment = .Center
backbutton.setImage(UIImage(named: "icon-backArrow-black"), forState: UIControlState.Normal)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//TODO: Remove adding of subviews etc
override func layoutSubviews() {
super.layoutSubviews()
label.frame = self.frame
backbutton.frame = CGRect(x: 16, y: 0, width: 20, height: self.frame.height)
self.addSubview(label)
self.addSubview(backbutton)
let border = CALayer()
let width = CGFloat(0.8)
//default gray color on UITableViewSeparator
border.borderColor = UIColor(red: 0.783922, green: 0.780392, blue: 0.8, alpha: 1.0).CGColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
@alonecuzzo
Copy link
Author

for the

required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
}

remove the fatalError and pop in super.init(coder: aDecoder) and then you can add your setup() function in there after the super.init(coder: aDecoder) gets called

@alonecuzzo
Copy link
Author

don't forget that layoutSubviews() can get called multiple times on a view, so we don't want to add subviews or do any sort of configuration other than reframing/repositioning in there... any border properties like color and size should be moved to the setup() function as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment