按钮被某些东西覆盖,无法正常工作

时间:2019-03-20 04:11:40

标签: ios swift iphone xcode uibutton

我创建了一个自定义视图,其中包含一个日期选择器和一个提交按钮。当我在视图控制器中创建并调用此视图时,该按钮无法立即起作用,因此必须先使用datepicker拨盘进行几次播放,然后才能起作用。我觉得我很陌生,因为我很陌生。任何帮助表示赞赏。

这是我的自定义视图代码

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)
    }

谢谢您的时间。

查看调试器: enter image description here

1 个答案:

答案 0 :(得分:0)

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

我看到的是约束错误

1)自定义视图的高度为80,但日期选择器高度为 50 ,并且从您的自定义视图的中心开始,因此它的y位置将为 15

2)提交按钮的高度为30,也位于35顶部,而日期选择器的底部则只有15个像素(由于中心Y)

解决方案:

您的约束应为

DatePicker =->开头(CustomView),结尾(CustomView),顶部(CustomView),底部(Submit Button)

自定义按钮 =->根据您的要求,开头,结尾,底部(CustomView),高度(30)