IOS按钮在viewDidLoad时候通过调整frame改变位置时候,按钮会失效

IOS按钮在viewDidLoad时候通过调整frame改变位置时候,按钮会失效。
具体情况如下:


界面视图,注册和登陆按钮就是出问题的两个按钮




代码里面btnX = 391 的时候,注册和登陆按钮在如图的位置,此时按钮有效,点击有反应


问题来了,代码里面btnX = 391+50的时候,按钮位置刚好调整在底部,但是两个按钮点击都没有任何反应。点其他位置也没反应。我这可能是什么问题??调试好几天了,求大神解救。

没积分了。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是使用 Swift 语言创建三个单选按钮,并为其添加鼠标悬停和点击时的颜色变化效果: ```swift import Cocoa class ViewController: NSViewController { var radioButton1: NSButton! var radioButton2: NSButton! var radioButton3: NSButton! override func viewDidLoad() { super.viewDidLoad() // 创建第一个单选按钮 radioButton1 = NSButton(frame: NSRect(x: 50, y: 100, width: 100, height: 30)) radioButton1.title = "Option 1" radioButton1.setButtonType(.radio) radioButton1.action = #selector(radioButtonClicked) radioButton1.target = self view.addSubview(radioButton1) // 创建第二个单选按钮 radioButton2 = NSButton(frame: NSRect(x: 50, y: 60, width: 100, height: 30)) radioButton2.title = "Option 2" radioButton2.setButtonType(.radio) radioButton2.action = #selector(radioButtonClicked) radioButton2.target = self view.addSubview(radioButton2) // 创建第三个单选按钮 radioButton3 = NSButton(frame: NSRect(x: 50, y: 20, width: 100, height: 30)) radioButton3.title = "Option 3" radioButton3.setButtonType(.radio) radioButton3.action = #selector(radioButtonClicked) radioButton3.target = self view.addSubview(radioButton3) } @objc func radioButtonClicked(sender: NSButton) { // 设置点击时的红色背景颜色 sender.layer?.backgroundColor = NSColor.red.cgColor } override func mouseEntered(with event: NSEvent) { // 设置鼠标悬停时的颜色 if let button = event.trackingArea?.owner as? NSButton { button.layer?.backgroundColor = NSColor.blue.cgColor } } override func mouseExited(with event: NSEvent) { // 恢复正常背景颜色 if let button = event.trackingArea?.owner as? NSButton { button.layer?.backgroundColor = nil } } } ``` 这段代码创建了一个 macOS 应用程序的视图控制器,并在其视图中添加了三个单选按钮。当鼠标经过按钮时,按钮的背景颜色变为蓝色,当点击按钮时,背景颜色变为红色。请将这段代码添加到你的项目中,并在需要的地方调用该视图控制器。记得要在 Main.storyboard 中将视图控制器连接到你的窗口!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值