3

I am working with UIDatePicker where i need to set borderWidth, borderColor and cornerRadius. But i can't make it work. I have masksToBounds to YES also.

dPicker.layer.masksToBounds=YES;
dateBtn.layer.borderWidth = 2.0;
dateBtn.layer.borderColor = [[UIColor redColor]CGColor];
dateBtn.layer.cornerRadius  =5.0;

Any solution please ?

Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49

3 Answers3

2

Apple does not intend you to modify the appearance of a date picker. If you were to contact DTS they would almost certainly tell you that a date picker's layers and subviews are private and should to be modified. To quote the docs:

You cannot customize the appearance of date pickers.

Thus you may be out of luck. This sort of change in behavior between OS versions is exactly the sort of thing that makes going beyond the public API of system components risky.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Might be you are right earlier it was worked but may be not in new OS http://stackoverflow.com/questions/20693607/setting-corner-radius-on-uidatepicker-with-a-background-color – kb920 Jun 06 '16 at 12:16
  • I have an app in production with use of modified date picker's layer - color border and rounded corners, everything is ok now :) @DuncanC – Evgeny Karkan Jun 06 '16 at 12:29
  • Yes, you are right but it is for the component of picker. Not for border or corner. I had problem with masksToBounds. Now it works. Thank you.@Duncan C – Jamshed Alam Jun 06 '16 at 12:50
2

Works great for me:

self.datePicker.layer.borderColor   = [UIColor grayColor].CGColor;
self.datePicker.layer.borderWidth   = 1.0f;
self.datePicker.layer.cornerRadius  = 5.0f;
self.datePicker.layer.masksToBounds = YES;

Try my code in given order of it lines. Date picker with color rounded border

Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38
1

The below code works for me just verified.

 dummyDatePicker.layer.masksToBounds = true;
 dummyDatePicker.layer.borderWidth = 2.0;
 dummyDatePicker.layer.borderColor = UIColor.purpleColor().CGColor;
 dummyDatePicker.layer.cornerRadius = 5.0;

enter image description here

but as @Duncan suggest it is not nice to customise default UI components as directed by Apple, I have had such an issue in the past when iOS 7 came out had to rewrite the whole UIDatePicker logic :-(

Community
  • 1
  • 1
Satheesh
  • 10,998
  • 6
  • 50
  • 93
  • @jamshed I guess I was the first one to post the screenshot okay no problem :( – Satheesh Jun 07 '16 at 10:11
  • Actually i did not check the screenshot priority. I checked the answer who give me a solution thats works perfect. But I realised you are very good. Next time i will definitely care about everything. Thanks again. – Jamshed Alam Jun 07 '16 at 10:40