admin管理员组文章数量:1122826
What I want: A SwiftUI style .padding
method in UIKit.
What I tried:
class Padding<T: UIView>: UIView {
var base: T { viewToPad }
let defaultPadding: UIEdgeInsets = .init(top: 8, left: 12, bottom: 8, right: 12)
private var viewToPad: T
init(view: T, padding: UIEdgeInsets? = nil) {
self.viewToPad = view
self.viewToPad.translatesAutoresizingMaskIntoConstraints = false
super.init(frame: .zero)
addSubview(viewToPad)
self.viewToPad.attachToContainer(side: .top, constant: padding?.top ?? defaultPadding.top)
self.viewToPad.attachToContainer(side: .bottom, constant: padding?.bottom ?? defaultPadding.bottom)
self.viewToPad.attachToContainer(side: .left, constant: padding?.left ?? defaultPadding.left)
self.viewToPad.attachToContainer(side: .right, constant: padding?.right ?? defaultPadding.right)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension UIView {
func padded<T: UIView>(with padding: UIEdgeInsets? = nil) -> Padding<T> {
return Padding(view: self as! T, padding: padding)
}
}
My issue: I can't make base
be the actual T
type:
let label = UILabel(text: "Instructions").padding()
label.base.textColor = UIColor.black // Value of type 'UIView' has no member 'textColor'
label.base
is of type UIView
here. Is there a way to make base
be of type UILabel
?
Thanks
What I want: A SwiftUI style .padding
method in UIKit.
What I tried:
class Padding<T: UIView>: UIView {
var base: T { viewToPad }
let defaultPadding: UIEdgeInsets = .init(top: 8, left: 12, bottom: 8, right: 12)
private var viewToPad: T
init(view: T, padding: UIEdgeInsets? = nil) {
self.viewToPad = view
self.viewToPad.translatesAutoresizingMaskIntoConstraints = false
super.init(frame: .zero)
addSubview(viewToPad)
self.viewToPad.attachToContainer(side: .top, constant: padding?.top ?? defaultPadding.top)
self.viewToPad.attachToContainer(side: .bottom, constant: padding?.bottom ?? defaultPadding.bottom)
self.viewToPad.attachToContainer(side: .left, constant: padding?.left ?? defaultPadding.left)
self.viewToPad.attachToContainer(side: .right, constant: padding?.right ?? defaultPadding.right)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension UIView {
func padded<T: UIView>(with padding: UIEdgeInsets? = nil) -> Padding<T> {
return Padding(view: self as! T, padding: padding)
}
}
My issue: I can't make base
be the actual T
type:
let label = UILabel(text: "Instructions").padding()
label.base.textColor = UIColor.black // Value of type 'UIView' has no member 'textColor'
label.base
is of type UIView
here. Is there a way to make base
be of type UILabel
?
Thanks
Share Improve this question edited Nov 21, 2024 at 9:38 Arjan asked Nov 21, 2024 at 9:30 ArjanArjan 17k5 gold badges33 silver badges41 bronze badges 1- Seen this? stackoverflow.com/questions/57334294/… – Dai Commented Nov 21, 2024 at 10:07
1 Answer
Reset to default 1Instead of being generic, padded
should return Padding<Self>
, but it cannot do that directly in a an extension of UIView
. You can work around this by introducing a protocol, and extending that protocol.
protocol Paddable: UIView {}
extension UIView: Paddable {}
extension Paddable {
func padded(with padding: UIEdgeInsets? = nil) -> Padding<Self> {
return Padding(view: self, padding: padding)
}
}
Now this is possible:
let label = UILabel().padded()
label.base.textColor = UIColor.black
本文标签: swiftKeep type of generic for later reuseStack Overflow
版权声明:本文标题:swift - Keep type of generic for later reuse - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312119a1934984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论