【问题标题】:SwiftUI Button with UIView带有 UIView 的 SwiftUI 按钮
【发布时间】:2020-11-11 03:13:39
【问题描述】:

我正在从一个名为 UIPillButton 的自定义 UIButton 类制作一个 SwiftUI 按钮。下面是创建 SwiftUI 按钮的代码:

Button(action: {
    print("Button tapped")
}) {
    PillButton()
        .padding(.top)
}

这是我创建一个名为 PillButton 的 SwiftUI 视图的类,它将我的自定义 UIButton 转换为:

struct PillButton: UIViewRepresentable {
    var ntPillButton = NTPillButton(type: .filled, title: "Start Test")
    
    func makeCoordinator() -> Coordinator { Coordinator(self) }
    
    class Coordinator: NSObject {
        var parent: PillButton
        
        init(_ pillButton: PillButton) {
            self.parent = pillButton
            super.init()
        }
    }

    func makeUIView(context: UIViewRepresentableContext<PillButton>) -> UIView {
        let view = UIView()
        view.addSubview(ntPillButton)
        
        NSLayoutConstraint.activate([
            ntPillButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            ntPillButton.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
        
        return view
    }

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PillButton>) {}
}

问题是如果我点击PillButton 本身,按钮不会运行该操作。仅当我选择PillButton 上方的缓冲区(填充)空间时才有效。我怎样才能做到这一点,以便我可以将自定义 PillButton 类用作普通的 SwiftUI 按钮?

【问题讨论】:

    标签: ios button uibutton swiftui uiviewrepresentable


    【解决方案1】:

    不清楚NTPillButton 是什么,但如果它是UIButton 的子类,那么下面的泛型方法演示(使用基础UIButton)应该清楚且适用。

    使用 Xcode 11.4 / iOS 13.4 测试

    下面给出了这个简单的用法

        PillButton(title: "Start Test") {
            print("Button tapped")
        }
        .frame(maxWidth: .infinity)       // << screen-wide
        .padding(.top)
    

    所以现在PillButton 演示本身:

    struct PillButton: UIViewRepresentable {
        let title: String
        let action: () -> ()
    
        var ntPillButton = UIButton()//NTPillButton(type: .filled, title: "Start Test")
    
        func makeCoordinator() -> Coordinator { Coordinator(self) }
    
        class Coordinator: NSObject {
            var parent: PillButton
    
            init(_ pillButton: PillButton) {
                self.parent = pillButton
                super.init()
            }
    
            @objc func doAction(_ sender: Any) {
                self.parent.action()
            }
        }
    
        func makeUIView(context: Context) -> UIButton {
            let button = UIButton(type: .system)
            button.setTitle(self.title, for: .normal)
            button.addTarget(context.coordinator, action: #selector(Coordinator.doAction(_ :)), for: .touchDown)
            return button
        }
    
        func updateUIView(_ uiView: UIButton, context: Context) {}
    }
    

    【讨论】:

    • 是的,行得通。但是,我的自定义 UIButton (NTPillButton) 仅适合文本的大小,因此按钮不会像我想要的那样跨越屏幕的宽度。有没有办法将 PillButton 固定在屏幕边缘?也许有一个 HStack?
    • @CalebRudnicki,只需使用 .frame 修饰符。查看更新。
    • 不完全是。 PillButton 继承的 UIButton 仅填充了适合按钮文本所需的空间量。我可以在 swiftui 代码中使按钮跨越整个宽度,还是必须在我的 UIButton 类中完成?
    猜你喜欢
    • 1970-01-01
    • 2012-09-04
    • 2020-07-25
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多