为什么[UITableView外观]弄乱了UIDatePicker?

时间:2014-01-27 14:29:37

标签: ios objective-c cocoa-touch uitableview uidatepicker

当将UIDatepicker呈现为UITextField子类的inputView时,我遇到了一个奇怪的问题。 发生的事情是提示器正在被呈现但是第二列被某些东西切断,导致在第三或第四个字符处截断月份名称。以下是此问题的screenshot

整个问题是在应用程序开始时生成的,同时设置了一些样式,特别是UITableView样式。负责此行为的代码行是这样的:

[[UITableView appearance] setBackgroundColor:[self greyPayAppColor]];

greyPayAppColor只是其他地方定义的一些自定义灰色。事实上,截图中显示的确切颜色涵盖了月份名称。 现在,我真的很困惑,因为我不知道UITableView外观消息如何搞乱UIDatePicker。另一件事是这个问题只发生在UIDatePicker用作文本字段的inputView,而不是由IB生成的常规视图。

为了复制目的,我正在使用我的UITexField子类。它生成两个视图,一个UIDatePicker和一个UIToolbar,将它们设置为文本字段inputView和inputAccessoryView。当按下“完成”和“取消”按钮时,它还使用委托进行通信。

.h文件:

#import <UIKit/UIKit.h>

@protocol DatePickerTextFieldDelegate <NSObject>

@optional
- (void)didDismissDatePickerWithDate:(NSDate *)date; // If date == nil => cancel was pressed
@end

@interface DatePickerTextField : UITextField
@property (nonatomic, weak) id<DatePickerTextFieldDelegate> datePickerDelegate;
@property (nonatomic, strong) NSDate *date;


@end

.m文件:

#import "DatePickerTextField.h"

@interface DatePickerTextField ()
@property (strong, nonatomic) UIToolbar *toolBar;
@property (strong, nonatomic) UIDatePicker *datePicker;
@end

@implementation DatePickerTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setup];
    }
    return self;
}

- (void)awakeFromNib
{
    [self setup];
}

- (void)setup
{
    self.datePicker = [self setupDatePicker];
    self.toolBar = [self setupToolBar];
    self.inputView = self.datePicker;
    self.inputAccessoryView = self.toolBar;
}

- (UIDatePicker *)setupDatePicker
{
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    return datePicker;
}

- (void)setDate:(NSDate *)date
{
    _date = date;
    if (_date) {
        self.text = [self localizedDateString:_date];
        [self.datePicker setDate:_date animated:NO];
    }
}

- (NSString *)localizedDateString:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    NSString *dateString = [dateFormatter stringFromDate:date];
    return dateString;
}

- (UIToolbar *)setupToolBar
{
    CGFloat width = [self superview].frame.size.width;
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
    UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelPressed)];
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed)];
    toolBar.items = @[cancel,extraSpace,done];
    return toolBar;
}

- (void)cancelPressed
{
    [self.datePickerDelegate didDismissDatePickerWithDate:nil];
}

- (void)donePressed
{
    [self.datePickerDelegate didDismissDatePickerWithDate:self.datePicker.date];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

1 个答案:

答案 0 :(得分:0)

我从来没有理解为什么会发生这种情况但我设法通过覆盖该特定VC中的外观调用来解决它,因为它不包含tableView,如下所示:

// override appearence
[[UITableView appearanceWhenContainedIn:[self.superview class], nil] setBackgroundColor:[UIColor clearColor]];

我仍然想了解UITableView和UIDatepicker之间的这种关系。如果有人知道,请发表评论!

感谢。