如果有人想知道如何更改UIDatePicker的背景颜色(围绕实际微调器的部分),请参阅下面的答案。如何更改UIDatePicker的颜色?
回答
要设置您想要的颜色,只需更改createCover
方法即可将cover
设置为您想要的任何颜色。
//CustomizableDatePicker.m
#import "CustomizableDatePicker.h"
#import <QuartzCore/QuartzCore.h>
#define TOP_HEIGHT 9.7f
#define BOTTOM_HEIGHT TOP_HEIGHT
#define DATE_AND_TIME_MODE_ACTUAL_PICKER_WIDTH 302.5f
#define DATE_MODE_ACTUAL_PICKER_WIDTH 280.0f
#define TIME_MODE_ACTUAL_PICKER_WIDTH 176.0f
@interface CustomizableDatePicker()
@property (nonatomic, weak) UIView *myLeftCover;
@property (nonatomic, weak) UIView *myRightCover;
@property (nonatomic, weak) UIView *myTopCover;
@property (nonatomic, weak) UIView *myBottomCover;
@end
@implementation CustomizableDatePicker
@synthesize myLeftCover = _myLeftCover, myRightCover = _myRightCover, myTopCover = _myTopCover, myBottomCover = _myBottomCover;
- (void) setDatePickerMode:(UIDatePickerMode)datePickerMode
{
[super setDatePickerMode:datePickerMode];
[self setNeedsLayout];
}
- (void) awakeFromNib
{
[self addCoverViews];
self.layer.cornerRadius = 10;
self.clipsToBounds = YES;
[self setNeedsLayout];
[self setNeedsDisplay];
}
- (id) init
{
if(self = [super init])
{
[self addCoverViews];
self.layer.cornerRadius = 10;
self.clipsToBounds = YES;
[self setNeedsLayout];
[self setNeedsDisplay];
}
return self;
}
/*
* Creates, but does NOT set the frames of, the 4 covers
*/
- (void) addCoverViews
{
UIView *left = [self createCover];
self.myLeftCover = left;
[self addSubview:left];
UIView *right = [self createCover];
self.myRightCover = right;
[self addSubview:right];
UIView *top = [self createCover];
self.myTopCover = top;
[self addSubview:top];
UIView *bottom = [self createCover];
self.myBottomCover = bottom;
[self addSubview:bottom];
}
/*
* Helper function to create one cover
*/
- (UIView *) createCover;
{
UIView *cover = [[UIView alloc] init];
cover.backgroundColor = [UIColor whiteColor];
cover.alpha = .55;
return cover;
}
- (void) layoutSubviews
{
float pickerWidth;
if(self.datePickerMode == UIDatePickerModeDateAndTime)
{
pickerWidth = DATE_AND_TIME_MODE_ACTUAL_PICKER_WIDTH;
}
else if(self.datePickerMode == UIDatePickerModeDate)
{
pickerWidth = DATE_MODE_ACTUAL_PICKER_WIDTH;
}
else if(self.datePickerMode == UIDatePickerModeTime)
{
pickerWidth = TIME_MODE_ACTUAL_PICKER_WIDTH;
}
// Set left frame
{
float xOrigin = 0;
float yOrigin = TOP_HEIGHT;
float width = (self.frame.size.width - pickerWidth)/2;
float height = self.frame.size.height - TOP_HEIGHT - BOTTOM_HEIGHT ;
self.myLeftCover.frame = CGRectMake(xOrigin, yOrigin, width, height);
}
// Set top frame
{
float xOrigin = 0;
float yOrigin = 0;
float width = self.frame.size.width;
float height = TOP_HEIGHT;
self.myTopCover.frame = CGRectMake(xOrigin, yOrigin, width, height);
}
// Set right frame
{
float xOrigin = (self.frame.size.width - pickerWidth)/2 + pickerWidth;
float yOrigin = TOP_HEIGHT;
float width = (self.frame.size.width - pickerWidth)/2;
float height = self.frame.size.height - TOP_HEIGHT - BOTTOM_HEIGHT ;
self.myRightCover.frame = CGRectMake(xOrigin, yOrigin, width, height);
}
// Set bottom frame
{
float xOrigin = 0;
float yOrigin = self.frame.size.height - BOTTOM_HEIGHT;
float width = self.frame.size.width;
float height = BOTTOM_HEIGHT;
self.myBottomCover.frame = CGRectMake(xOrigin, yOrigin, width, height);
}
}
@end
//CustomizableDatePicker.h
#import <UIKit/UIKit.h>
/*
* Allows for changing the color of UIDatePickers...Pretty useful, huh? :)
*/
@interface CustomizableDatePicker : UIDatePicker
@end
一直在寻找像这样的东西。发现了一个覆盖类,但这大大减缓了加载时间,所以我真的很想使用它。我对CoreAnimation不太了解,所以这就是我在这里问的原因... 有没有什么方法可以四角?所以选择器有一个更自然的外观,就像默认的那样?如果你能帮我解决问题,那将会很棒,或者至少指导我如何自己解决问题;我不避免学习。 谢谢! – Roland 2013-04-07 00:39:51
我真的没有时间给你一个完整的解释,但这是你如何可以使'UIView'的圆角:http://stackoverflow.com/questions/1509547/uiview-with-rounded-corners – Nosrettap 2013-04-07 02:02:01
悲伤我没有正确地问,这是有点迟了:)内角必须是圆形(向内)。基本的UIView圆角并不是很有用。那么,感谢您花时间回答,如果您将来有空闲了一点点,那么调整此课程并将其作为默认的一种标准是非常棒的。再次感谢! – Roland 2013-04-07 10:35:35
- 1. 如何更改UIPickerView和UIDatePicker的字体颜色?
- 2. UIDatePicker更改“today”日期的文本颜色
- 3. 如何更改光标颜色而不更改文字颜色?
- 4. 如何更改sbt颜色?
- 5. 如何更改ActionBar颜色
- 6. 如何更改createCircularReveal()颜色?
- 7. 如何更改layer.border.top颜色?
- 8. 如何更改CircularFillableLoaders颜色?
- 9. 如何更改CCTexture2D颜色
- 10. UNITY - 如何更改颜色?
- 11. 如何更改ActionBar颜色?
- 12. 如何更改UITabBar颜色?
- 13. UITableView如何更改颜色?
- 14. 如何更改ttk.Separator颜色
- 15. 如何更改JProgressBar颜色?
- 16. 如何更改颜色?
- 17. 如何更改颜色
- 18. 如何更改GroupStyle颜色?
- 19. 如何更改QIcon颜色?
- 20. raphael如何更改颜色
- 21. 如何更改JLabel颜色
- 22. 如何更改TabPanel颜色?
- 23. 如何更改UIAlertController的色彩颜色?
- 24. UIDatePicker突出显示今天的日期 - 我如何更改颜色?
- 25. IOS-如何更改UIdatepicker中的文本颜色和选定的日期边界颜色?
- 26. Xcode - UIDatePicker背景颜色
- 27. iOS 7反转UIDatePicker颜色
- 28. 更改颜色
- 29. 更改颜色
- 30. 更改颜色
感谢您的信息 – 2012-08-10 15:50:00