【问题标题】:Button is being overlapped by something and not functioning properly按钮被某些东西重叠并且无法正常工作
【发布时间】:2019-08-10 17:13:06
【问题描述】:

我创建了一个自定义视图,其中包含一个日期选择器和一个提交按钮。当我在视图控制器中创建并调用此视图时,该按钮无法立即工作,我必须使用日期选择器拨盘几次才能工作。我觉得我以某种方式搞砸了我的约束,因为我对它还很陌生。任何帮助表示赞赏。

这是我的自定义视图代码

func setupDatePicker() {
    datepicker = UIDatePicker()
    datepicker.minimumDate = Date()
    datepicker.addTarget(self, action: #selector(datePicker(sender:)), for: .valueChanged)
    datepicker.setValue(Colors.starYellow, forKey: "textColor")
}

func setupButton(){
    submitButton = CustomButton()
    submitButton.setTitle("Submit", for: .normal)
    submitButton.addTarget(self, action: #selector(dateSubmission(sender:)), for: .touchUpInside)

}

func addingSubviews() {
    setupButton()
    addSubview(submitButton)
    bringSubviewToFront(submitButton)
    setupDatePicker()
    addSubview(datepicker)
}



func setupConstraints(){

    addingSubviews()
    //Custom View Constraints
    translatesAutoresizingMaskIntoConstraints = false
    leadingAnchor.constraint(equalTo: superview!.leadingAnchor, constant: 20).isActive = true
    trailingAnchor.constraint(equalTo: superview!.trailingAnchor, constant: -20).isActive = true
    heightAnchor.constraint(equalToConstant: 80).isActive = true
    centerYAnchor.constraint(equalTo: superview!.centerYAnchor).isActive = true
    backgroundColor = Colors.darkBackground
    layer.cornerRadius = 15

    //DatePicker Constraints
    datepicker.translatesAutoresizingMaskIntoConstraints = false
    datepicker.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 10).isActive = true
    datepicker.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -10).isActive = true
    datepicker.heightAnchor.constraint(equalToConstant: 50).isActive = true
    datepicker.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true


    // Button Constraints

    submitButton.translatesAutoresizingMaskIntoConstraints = false
    submitButton.leadingAnchor.constraint(equalTo: datepicker.leadingAnchor, constant: 100).isActive = true
    submitButton.trailingAnchor.constraint(equalTo: datepicker.trailingAnchor, constant: -100).isActive = true
    submitButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
    submitButton.bottomAnchor.constraint(equalTo: datepicker.bottomAnchor, constant: 35).isActive = true



}


override func didMoveToSuperview() {
   setupConstraints()
}


@objc func datePicker(sender: UIDatePicker){
    self.datepicked = sender.date
    print("Date selected: \(datepicked)")
}

这是未被调用的函数

    @objc func dateSubmission(sender: CustomButton){
        sender.shake()
        let currentDate = Date()
        let interval = datepicked?.timeIntervalSince(currentDate)
        print(interval)
        let notifcation = UNMutableNotificationContent()
        notifcation.title = notificationTitle ?? "title empty"
        notifcation.subtitle = "test"
        notifcation.body = "test2"
        notifcation.badge = 1

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval!, repeats: false)
        let request = UNNotificationRequest(identifier: "taskReminder", content: notifcation, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }


}

这就是我在视图控制器中创建自定义视图的方式,我想在其中显示视图。

var customView: CustomView!
 func createReminderView(indexPath: IndexPath){
    let task = tasks[indexPath.row]

    customView = CustomView()
    customView.notificationTitle = task.task
    view.addSubview(customView)
    view.bringSubviewToFront(customView)
    }

感谢您的宝贵时间。

查看调试器:

【问题讨论】:

  • 您能否发布在调试器上看到的视图层次结构的屏幕截图?
  • 检查提交按钮上方是否有任何内容。如果有什么,你需要更新你的约束。

标签: ios swift iphone xcode uibutton


【解决方案1】:

我敢肯定,如果您设置 clipsToBounds = true,您将无法看到该按钮。因为您的自定义视图没有足够的高度

我能看到的是约束中的错误

1) 自定义视图高度为 80,但 Datepicker 高度为 50 并从自定义视图的中心开始,因此它的 y 位置将是 15

2) 提交按钮高度是 30 ,也是顶部 35 来自日期选择器,底部只有 15 个像素(因为中心 Y)

解决方案:

你的约束应该是

DatePicker =--> 前导(CustomView)、尾随(CustomView)、顶部(CustomView)、底部(提交按钮)

自定义按钮 =-->前导,尾随,根据您的要求,底部(自定义视图),高度(30)

【讨论】:

  • 请告诉我需要应用的更正
  • 我应用了更改,它改变了它的外观,并且按钮与日期选择器分开,但它并没有解决我的问题。按钮仍然是静态的,没有调用函数。
  • @CharlesXavier 共享屏幕截图以获得预期的输出。以及您如何使用 CustomView ?,您需要在类级别创建属性,否则您的按钮事件永远不会调用,因为您的自定义视图实例已释放
  • 我正在调用在 UIContextualAction 操作中创建视图的函数
  • 当我玩几次日期选择器的拨盘时,按钮会起作用。我不断调整值,然后按下按钮,直到它工作并打印我需要的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
相关资源
最近更新 更多