Search code examples
iosiphoneswiftuipickerviewuicontrol

How to create a simple(custom) time picker with just two colomns, hour and minute?


I want to make a simple TimePicker like this :

enter image description here

If I use UITableView then how do I know that middle row/cell is selected ? Also the last row/cell will never be in middle. Apart from that, It has to be two tableViews as both hour selection and minute selection needs to be separate. If I use two scrollViews , still how to figure out when an item is exactly in the middle and should be considered as selected item.

I could not find any customization options in the default date time picker. Secondly , my picker is very simple one, yet I am not able to find a proper method to create this type of picker. I cannot use the default picker, because it has different design which is not what I want.

Any help is appreciated. Thanks


Solution

  • If I understand you right, you want only time in your picker and design it? From storyboard you can select wheather it is "Time", "Date", "Date & time" etc. Just slect your picker and from right select attributes.

    enter image description here

    But for design I suggest you to try these lines of codes:

    datePicker.backgroundColor = UIColor.whiteColor() //It is for background color
    
    datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")//It is for text color
    
    self.datePicker.performSelector("_setHighlightColor:", withObject:Constants.Colors.mainHeaderColor) //This should be higlight color
    

    And I suggest you to read this topic.