vikmeup / SCLAlertView-Swift

Beautiful animated Alert View. Written in Swift
MIT License
5.32k stars 764 forks source link

Can't embed a UIDatePicker inside of alert? #297

Closed LukeSmith16 closed 12 months ago

LukeSmith16 commented 7 years ago

I'm trying to implement a UIDatePicker into my custom alert but failing to do so. This is the code i'm using to try and accomplish it: let appearance = SCLAlertView.SCLAppearance(kCircleIconHeight: 60.0, kTitleFont: UIFont(name: "HelveticaNeue", size: 20)!, kTextFont: UIFont(name: "HelveticaNeue-Light", size: 14)!, kButtonFont: UIFont(name: "HelveticaNeue-Light", size: 14)!, showCloseButton: false, showCircularIcon: true) let alert = SCLAlertView(appearance: appearance)

   ' let datePicker = UIDatePicker()

    let subview = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    datePicker.layer.frame = CGRect(x: subview.layer.frame.midX, y: subview.layer.frame.midY, width: 100, height: 100)
    subview.addSubview(datePicker)

    alert.customSubview = subview `

When the alert comes up the date picker is way out of frame on the alert, i can still however interact with it. Could this be a problem with how i have set up the positioning? Also i do not have any constraints implemented, i'm not sure if this would affect it. Thanks.

algrid commented 7 years ago

This works for me:

        let icon = UIImage(named:"custom_icon.png")
        let color = UIColor.orange

        let datePicker = UIDatePicker()

        let appearance = SCLAlertView.SCLAppearance(kWindowWidth: datePicker.frame.size.width + 28.0)

        let alert = SCLAlertView(appearance: appearance)

        alert.customSubview = datePicker

        _ = alert.showCustom("Some title", subTitle: "", color: color, icon: icon!)