I am subclassing UIButton, trying to align them through programmatically but it does not working. The same thing i can align them in stoaryboard . Why its not working?
See my code below:
@IBDesignable
class AddProfilePictureView: UIButton {
open var bottomLineLayer : CALayer?
@IBInspectable var textColor:UIColor = .gray{
didSet{ self.titleLabel?.textColor = textColor }
}
@IBInspectable var leftHandImage: UIImage? {
didSet {
leftHandImage = leftHandImage?.withRenderingMode(.alwaysOriginal)
setupImages()
}
}
@IBInspectable var rightHandImage: UIImage? {
didSet {
rightHandImage = rightHandImage?.withRenderingMode(.alwaysOriginal)
setupImages()
}
}
@IBInspectable var bottomLineColor: UIColor = UIColor.lightGray {
didSet {
addBottomLine()
}
}
@IBInspectable var imageSize: CGFloat = 20{
didSet {
setupImages()
}
}
@IBInspectable var titlePadding: CGFloat = 25{
didSet {
alignment()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
alignment()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
alignment()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
alignment()
}
override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
var tempRect = contentRect
tempRect.origin.x = titlePadding
return tempRect
}
override func layoutSubviews() {
super.layoutSubviews()
addBottomLine()
}
func alignment(){
contentVerticalAlignment = .bottom
}
func addBottomLine(){
// if bottomLineLayer == nil{
for layer in self.layer.sublayers ?? [] {
if layer.name == "bottom" {
layer.removeFromSuperlayer()
}
}
bottomLineLayer = CALayer()
bottomLineLayer?.name = "bottom"
bottomLineLayer?.frame = CGRect(x: 0.0, y: self.frame.height + 5, width: self.frame.width, height: 1.0)
bottomLineLayer?.backgroundColor = bottomLineColor.cgColor
self.layer.addSublayer(bottomLineLayer ?? CALayer())
// }
}
func setupImages() {
if let rightImage = rightHandImage {
for views in self.subviews{
if views.tag == 100{
views.removeFromSuperview()
}
}
let rightImageView = UIImageView(image: rightImage)
rightImageView.tag = 100
rightImageView.tintColor = .black
self.addSubview(rightImageView)
rightImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
rightImageView.trailingAnchor.constraint(equalTo: self.trailingAnchor,constant: 0),
// rightImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
rightImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0),
rightImageView.widthAnchor.constraint(equalToConstant: imageSize),
rightImageView.heightAnchor.constraint(equalToConstant: imageSize)
])
}
if let rightImage = leftHandImage {
for views in self.subviews{
if views.tag == 101{
views.removeFromSuperview()
}
}
let rightImageView = UIImageView(image: rightImage)
rightImageView.tintColor = .black
rightImageView.tag = 101
self.addSubview(rightImageView)
rightImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
rightImageView.leadingAnchor.constraint(equalTo: self.leadingAnchor,constant: 0),
// rightImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
rightImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0),
rightImageView.widthAnchor.constraint(equalToConstant: imageSize),
rightImageView.heightAnchor.constraint(equalToConstant: imageSize)
])
}
}
}
This line does nothing
contentVerticalAlignment = .bottom
Output:
The problem is you are not calling super method of layoutSubviews. Try this
override func layoutSubviews() {
super.layoutSubviews()
addBottomLine()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.