【问题标题】:How to inherit from a SwiftUI Button如何从 SwiftUI 按钮继承
【发布时间】:2019-11-17 02:58:37
【问题描述】:

是否可以从 SwiftUI 中的 Button 继承?我厌倦了它,每次都会出错,因为 Button 期待某种通用参数。

import SwiftUI

struct ButtonSubClass: Button<Label: View> {
    var body: some View {
        Text("Hello, World!")
    }
}

XCode 会自动添加 &lt;Label: View&gt; 部分,但我不知道这意味着什么,或者我实际上需要在大括号中添加什么。 我试过Button&lt;View&gt;,但后来我收到错误Protocol type 'View' cannot conform to 'View' because only concrete types can conform to protocols

【问题讨论】:

标签: swift swiftui


【解决方案1】:

Button 是一个结构,不能被子类化。如果您尝试使用类似Button 的属性创建自定义View,但以某种方式进行了自定义,则可以应用视图修饰符或使用ButtonStyle 协议。

如果你能提供一些你想要完成的细节,我会尝试添加一些示例代码。

【讨论】:

  • 如果一些疯狂的 UI 设计师要求我们为 Button 添加一些 Android 风格的动画怎么办?
猜你喜欢
  • 2020-07-20
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
相关资源
最近更新 更多