문제

I have a UIDatePicker in my view and have set the background color of the UIDatePicker:

self.datePicker.backgroundColor = [UIColor lightTextColor];
self.datePicker.layer.cornerRadius = 10;

This successfully puts a background behind the UIDatePicker (which in iOS7 is essentially transparent) but fails to make the rounded corners for the background that I am looking for (I do this same thing for an image on the screen and it works perfectly).
It seems that the corner radius doesn't affect the background color.
Is there a way to fix this problem by setting a corner radius for the background color (or any another solution).

The reason I want to do this is because the ordinary UIDatePicker looks awkward in the view I have constructed and looks much better with a background color.
However, all the other items in the view have rounded corners and I want the UIDatePicker to match them.

Thanks.

도움이 되었습니까?

해결책

You have to add layer.masksToBounds=YES;

Try this,

self.datePicker.backgroundColor = [UIColor lightTextColor];
self.datePicker.layer.cornerRadius = 10;
self.datePicker.layer.masksToBounds=YES;

다른 팁

UIBuilder Swift 5 solution:

See picture, it's the same as @ToseefKhiji's solution but even easier to execute for fans of UIBuilder controls.

enter image description here

This is how it comes out (I have added other features to the picker programmatically, such as borderWidth, color, etc...)

enter image description here

UIBuilder is underappreciated by Swift/iOS coders as everyone has rushed to SwiftUI as the new new thing.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top