2014-01-27 84 views
1

當我把一個UIDatepicker作爲一個UITextField子控件的inputView呈現時,我有一個奇怪的問題。 發生了什麼事情是,選擇器正在呈現,但第二列正在削減的東西導致月份名稱截斷在第三或第四個字符。這裏是這個問題的一個screenshot爲什麼[UITableView外觀]搞亂了UIDatePicker?

整個問題產生於應用程序的開始,同時設置一些樣式,特別是UITableView樣式。負責這一行爲這行代碼是這一個:

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

greyPayAppColor是別處定義只是一些自定義的灰色。事實上,這是確切的顏色顯示在屏幕上的月份名稱。 現在,我真的很困惑,因爲我不知道如何UITableView外觀消息可以搞砸UIDatePicker。另一件事是,這個問題只發生在用作文本字段的輸入視圖的UIDatePicker上,而不是由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 

回答

0

我永遠無法理解爲什麼會這樣,但我設法通過在特定VC重寫外觀呼叫繞過它因爲它不包含tableView,像這樣:

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

我還是想了解UITableView和UIDatepicker之間的這種關係。如果有人知道請發表評論!

謝謝。

相關問題