Button in center of cell programatically

Button in center of cell programatically



I am trying to make it so that there is a 'Reset Progress' button inside the centre of a cell. Other posts I have found only show how to add a button on the left side or the right side, or require prior steps in the Interface Builder.



How can I add a button in the centre of a cell programmatically without any steps in the Interface Builder?





Calculate the centre point of the superview (container view) by dividing its height and width by two, then compensate for the buttons frame size. Tada.
– Woodstock
Aug 30 at 14:48





Or learn about how autolayout works and use that to position the button.
– Upholder Of Truth
Aug 30 at 14:58




2 Answers
2


class YourCell: UITableViewCell

override init(style: UITableViewCellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)

addSubview(button)


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


lazy var button: UIButton =
let button = UIButton()
button.backgroundColor = .blue
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
return button
()

override func layoutSubviews()
super.layoutSubviews()

button.frame = CGRect(x: 0, y: 0, width: 150, height: 50)
button.center = self.center


@objc func buttonAction()
print("Button has been pressed")




Button.centreXAxisInSuperView()



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)