简体   繁体   English

CATransform3DMakeTranslation后无法调用NSButton

[英]NSButton not callable after CATransform3DMakeTranslation

I have a strange issue never seen on iOS but present on macOS Swift development. 我有一个奇怪的问题,从未在iOS上见过,但出现在macOS Swift开发中。

I have a NSButton which is added with an addSubview method to a custom NSView . 我有一个NSButton ,它通过addSubview方法添加到自定义NSView The button has a target function. 该按钮具有目标功能。 It is impossible to click on this button when it's added to the customView . 将其添加到customView无法单击该按钮。 If I add it to the global "view" works fine directly. 如果我将其添加到全局“视图”中,则可以直接正常工作。

Any idea why this could be? 知道为什么会这样吗?

//Infos View
self.infosView.wantsLayer = true
self.infosView.layer?.backgroundColor = NSColor.darkGray.cgColor

self.view.addSubview(self.infosView)

 //TestButton
let myButtonRect = CGRect(x: 100, y: 100, width: 200, height: 200)
self.testButton = NSButton.init(frame: myButtonRect)

// !!!!
// this line doesn't work
// !!!!
self.infosView.addSubview(testButton)

// !!!!
// but this line works fine
// !!!!
self.view.addSubview(testButton)

self.testButton.target = self
self.testButton.action = #selector(ViewController.printSomething)

Update : The testButton is not clickable after a CATransform 更新: CATransform之后,testButton不可单击

self.infosView.layer?.transform = CATransform3DMakeTranslation(500, 0, 0)

Doesn't work after this.. 此后不起作用。

I paste here the answer of this question. 我在这里粘贴这个问题的答案。 The problem was the constraints of the NSView. 问题是NSView的约束。

1- Create a NSLayoutConstraints() variable 1-创建一个NSLayoutConstraints()变量

2- Set the constraints of the NSView 2-设置NSView的约束

self.infosView.translatesAutoresizingMaskIntoConstraints = false
        self.leadingConstraint = self.infosView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: -500)
        self.leadingConstraint.isActive = true
        self.infosView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
        self.infosView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0, constant: 500).isActive = true
        self.infosView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 0, constant: (NSScreen.main?.frame.height)!).isActive = true

3 - Then when I want to update the NSView which contains the NSButton 3-然后,当我想更新包含NSButton的NSView时

self.leadingConstraint.constant = 0

runAnimation(duration: 0.5, {
    self.testButton.superview?.layoutSubtreeIfNeeded()

    self.infosViewOpened = true

})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM