简体   繁体   English

iOS按钮在自定义视图中不起作用

[英]iOS Button not work in custom view

There are two custom views - viewA and viewB.ViewB is a small view added in viewA. 有两个自定义视图-viewA和viewB.ViewB是在viewA中添加的一个小视图。 At first viewA is filled in the whole screen. 首先,在整个屏幕中填充视图A。 ViewB is located in the bottom of viewA(out of the screen). ViewB位于viewA的底部(屏幕外)。 When click a button in viewA, viewB bottom constant of constraints will be -100, thus viewB will display in the bottom of the screen. 当单击viewA中的按钮时,viewB约束的底部常量将为-100,因此viewB将显示在屏幕底部。 But there is a button in viewB did not response its selector. 但是viewB中有一个按钮没有响应其选择器。 Here is my code: 这是我的代码:

In ViewA 在ViewA中

let viewB: ViewB = {
    let view = ViewB(
    view.setup()
    return view
}()

viewB constraints: viewB约束:

    viewB.snp.makeConstraints { (make) in
        make.trailing.leading.equalTo(self)
        make.height.equalTo(100)
        make.top.equalTo(self.snp.bottom)
    }

when click button to arouse viewB 当单击按钮引起视图B时

            UIView.animate(withDuration: 0.5) {
            self.snp.updateConstraints({ (make) in
                make.bottom.equalTo(-100)
            })
        }
        self.layoutIfNeeded()

In viewB: 在viewB中:

class ViewB: UIView {

let b: UIButton = {
    let button = UIButton(type: UIButtonType.custom)
    button.addTarget(self, action: #selector(btnClicked), for: UIControlEvents.touchUpInside)
    button.backgroundColor = UIColor.green
    button.setTitle("Button", for: .normal)
    return button
}()


func btnClicked() {
   print("btnClicked")
}

func setup() {
    addSubview(b)
    b.snp.makeConstraints { (make) in
        make.leading.top.bottom.equalTo(self)
        make.width.equalTo(100)
    }

}
}

查看调试器

Your code by itself works fine. 您的代码本身可以正常工作。 So the issue is probably something like the button being overlaid by another view which takes the touch actions. 因此,问题可能出在类似按钮被另一个执行触摸操作的视图所覆盖的事情上。 You can test this using the view debugger or provide a link to your project so one of us can take a look to see what is going on. 您可以使用视图调试器对此进行测试,也可以提供指向您项目的链接,以便我们中的一个可以看看发生了什么。

i think , your view is going out of its super View or Any other view is coming on its upper layer. 我认为,您的视图将超出其超级视图,或者任何其他视图将进入其上层。 please see by this 请看这里

superview.clipsToBounds :  YES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在视图中添加的事件按钮不起作用,iOS - Event button in view added not work, iOS iOS Swift 5 无法在自定义视图中禁用按钮 - iOS Swift 5 unable to disable button in custom view iOS自定义视图(.XIB)按钮绑定不起作用 - iOS Custom View (.XIB) Button Bind Not Working iOS:自定义后退按钮不起作用 - iOS: Custom back button doesn't work iOS自定义.xib视图按钮点击在视图控制器中检测(Swift) - iOS Custom .xib view button tap detect in view controller (Swift) iOS:UISplitViewController显示/隐藏主视图按钮不起​​作用 - iOS: UISplitViewController show/hide master view button doesn't work 如何在iOS中的MPMoviePlayerContoller视图上添加自定义右栏按钮 - how to add custom right bar button on MPMoviePlayerContoller view in iOS 创建类似于新的iOS 6按钮共享的自定义共享视图 - Creating a custom sharing view similar to new iOS 6 button sharing iOS SwiftUI - NavigationView NavigationLink:使用自定义按钮关闭视图 - iOS SwiftUI - NavigationView NavigationLink: Close view with custom Button iOS:如何在viewForHeaderInSection中添加滑动以删除按钮到自定义视图 - iOS: How to add swipe to delete button to a custom view in viewForHeaderInSection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM