2013-12-08 78 views
1

我有在其上一個UIPickerView其需要從屏幕的下半部分向上滑動的圖控制器(VC)。iOS7-iPhone呈現視圖控制器 - UIPickerView

使用:

ViewControlelr *vc = [self.storyboard instantiateViewControllerWithIdentifier: @"VC"]; 
[self presentViewController:vc animated:YES completion:nil] 

使得fullscreeen

頁面顯示香港專業教育學院讀的是把UIPickerViews在活動表是不是一個好主意了,有什麼建議?

回答

1

香港專業教育學院讀的是把UIPickerViews在活動表是不是一個好主意 了,有什麼建議?

誠實地說,這絕不是一個好主意,但它變成了一種地下標準。如果您在用戶設置文本字段作爲firstResponder後顯示出來,可以設置UIPickerView爲的UITextField的inputView。如果你想在它上面有一個工具欄,創建一個並將其設置爲UITextField的inputAccessoryView。

如果不是這種情況,我建議建立一個UIView容器,以便容納UIPickerView和其y設置爲屏幕的高度。當您想要顯示它時,使用UIView動畫將y更改爲屏幕高度減去容器高度。

或者你可以只是做同樣的事情,沒有一個UIView容器,但你可能會失去對你要如何呈現UIPickerView因爲它的大小是固定的一些控制。

編輯1:

我創建了擁有一個UIPickerView雖然它似乎只呈現在肖像模式下正確像樣的容器。隨意擴大它,使其方向友好。

// 
// AbstractPickerPresenter.h 
// ShowDatePickerView 
// 
// Created by Christopher John Morris on 10/10/13. 
// Copyright (c) 2013 Christopher John Morris. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@protocol PickerPresenterDelegate; 

@protocol PickerPresenterButtonDelegate; 

@interface PickerPresenter : UIView { 
    UIView *pickerContainer; 
    id<PickerPresenterButtonDelegate>buttonDelegate; 
} 

/* 
* Helper constant used for measuring frames 
*/ 
@property (nonatomic, readonly) CGFloat pickerHeight; 

/* 
* Helper constant used for measuring frames 
*/ 
@property (nonatomic, readonly) CGFloat navBarHeight; 

/* 
* Automatically hides view when Cancel is clicked. 
* Default is YES 
*/ 
@property (nonatomic) BOOL hideOnDoneClicked; 

/* 
* Automatically hides view when Done is clicked. 
* Default is YES 
*/ 
@property (nonatomic) BOOL hideOnCancelClicked; 

/* 
* Raises the view into visible position 
*/ 
- (void) show; 

/* 
* Removes the view out of visible position 
*/ 
- (void) hide; 

/* 
* Sets titles for buttons. Size of buttons adujst based on textsize 
*/ 
- (void)setCancelButtonTitle:(NSString *)title; 
- (void)setDoneButtonTitle:(NSString *)title; 

@end 

/* 
* Delegate that is used strictly by subclasses to react 
* to button presses. 
*/ 
@protocol PickerPresenterButtonDelegate 

@required 
- (void) didCancel; 
- (void) didSave; 

@end 

... 

// 
// AbstractPickerPresenter.m 
// ShowDatePickerView 
// 
// Created by Christopher John Morris on 10/10/13. 
// Copyright (c) 2013 Christopher John Morris. All rights reserved. 
// 

#import "PickerPresenter.h" 

@interface PickerPresenter() 

/* 
* Tint color of buttons 
*/ 
@property (nonatomic, strong) UIColor *tintColor; 

/* 
* Cancel button 
*/ 
@property (nonatomic, strong) UIButton *cancelButton; 

/* 
* Done button 
*/ 
@property (nonatomic, strong) UIButton *doneButton; 

/* 
* Black facade that appears behind the picker container 
* and is the size of the current UIWindow 
*/ 
@property (nonatomic, strong) UIView *facadeView; 

/* 
* Helper constant used to make animations consistent 
*/ 
@property (nonatomic, readonly) NSTimeInterval animationInterval; 

@end 

@implementation PickerPresenter 

- (id)init { 
    self = [super init]; 
    if (self) { 
     [self initializeView]; 
     [self initializeViewContainer]; 
     [self initializeCancelButton]; 
     [self initializeDoneButton]; 
    } 
    return self; 
} 

- (void) initializeView { 
    [self setHidden:YES]; 
    CGFloat height = [[UIScreen mainScreen] bounds].size.height; 
    self.frame = CGRectMake(0, height, [[UIScreen mainScreen] bounds].size.width, height); 
    self.backgroundColor = [UIColor clearColor]; 
    self.tintColor = [[[[UIApplication sharedApplication] delegate] window] tintColor]; 
    self.facadeView = [[UIView alloc] initWithFrame:self.frame]; 
    self.facadeView.backgroundColor = [UIColor blackColor]; 
    self.facadeView.alpha = 0.4; 
    [self addSubview:self.facadeView]; 
    self.hideOnDoneClicked = YES; 
    self.hideOnCancelClicked = YES; 
} 

- (void) initializeViewContainer { 
    CGFloat containerHeight = (self.pickerHeight + self.navBarHeight); 
    pickerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 
                   self.frame.size.height + containerHeight, 
                   self.frame.size.width, 
                   containerHeight)]; 
    pickerContainer.backgroundColor = [UIColor whiteColor]; 
    [self addSubview:pickerContainer]; 
} 

- (void) initializeCancelButton { 
    self.cancelButton = [[UIButton alloc] init]; 
    [self.cancelButton setTitleColor:self.tintColor forState:UIControlStateNormal]; 
    [self.cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; 
    [self.cancelButton setTitle:@"Cancel" forState:UIControlStateNormal]; 
    self.cancelButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:20]; 
    CGSize size = [self.cancelButton.titleLabel.text sizeWithFont:self.cancelButton.titleLabel.font]; 
    self.cancelButton.frame = CGRectMake(10, 0, size.width, self.navBarHeight); 
    [self.cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside]; 
    [pickerContainer addSubview:self.cancelButton]; 
} 

- (void) cancel { 
    [buttonDelegate didCancel]; 
} 

- (void) done { 
    [buttonDelegate didSave]; 
} 

- (void) initializeDoneButton { 
    self.doneButton = [[UIButton alloc] init]; 
    [self.doneButton setTitleColor:self.tintColor forState:UIControlStateNormal]; 
    [self.doneButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; 
    [self.doneButton setTitle:@"Done" forState:UIControlStateNormal]; 
    self.doneButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:20]; 
    [self.doneButton addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside]; 
    CGSize size = [self.doneButton.titleLabel.text sizeWithFont:self.doneButton.titleLabel.font]; 
    self.doneButton.frame = CGRectMake(self.frame.size.width - (size.width + 10), 0, size.width, self.navBarHeight); 
    [pickerContainer addSubview:self.doneButton]; 
} 

- (void)setCancelButtonTitle:(NSString *)title { 
    [self.cancelButton setTitle:title forState:UIControlStateNormal]; 
    CGSize size = [self.cancelButton.titleLabel.text sizeWithFont:self.cancelButton.titleLabel.font]; 
    self.cancelButton.frame = CGRectMake(10, 
             0, 
             size.width, 
             self.cancelButton.frame.size.height); 
} 

- (void)setDoneButtonTitle:(NSString *)title { 
    [self.doneButton setTitle:title forState:UIControlStateNormal]; 
    CGSize size = [self.doneButton.titleLabel.text sizeWithFont:self.doneButton.titleLabel.font]; 
    self.doneButton.frame = CGRectMake(self.frame.size.width - (size.width + 10), 
             self.doneButton.frame.origin.y, 
             size.width, 
             self.doneButton.frame.size.height); 
} 

- (void) show { 
    self.alpha = 0.0; 
    self.frame = CGRectMake(self.frame.origin.x, 
          self.frame.origin.y - self.frame.size.height, 
          self.frame.size.width, 
          self.frame.size.height); 
    self.facadeView.frame = CGRectMake(self.frame.origin.x, 
             self.frame.origin.y, 
             self.frame.size.width, 
             self.frame.size.height); 
    [[[UIApplication sharedApplication] keyWindow] addSubview:self]; 
    [UIView animateWithDuration:self.animationInterval animations:^{ 
     [self setHidden:NO]; 
     self.alpha = 1.0; 
    } completion:^(BOOL finished) { 
     [UIView animateWithDuration:self.animationInterval animations:^{ 
      pickerContainer.frame = CGRectMake(pickerContainer.frame.origin.x, 
               self.frame.size.height - pickerContainer.frame.size.height, 
               pickerContainer.frame.size.width, 
               pickerContainer.frame.size.height); 
     } completion:nil]; 
    }]; 
} 

- (void) hide { 
    CGFloat height = [[UIScreen mainScreen] bounds].size.height; 
    [UIView animateWithDuration:self.animationInterval animations:^{ 
     pickerContainer.frame = CGRectMake(0, 
              self.frame.size.height + pickerContainer.frame.size.height, 
              self.frame.size.width, 
              pickerContainer.frame.size.height); 
    } completion:^(BOOL finished) { 
     [UIView animateWithDuration:self.animationInterval animations:^{ 
      self.alpha = 0.0; 
     } completion:^(BOOL finished) { 
      [self setHidden:YES]; 
      [self removeFromSuperview]; 
      self.frame = CGRectMake(self.frame.origin.x, 
            height, 
            self.frame.size.width, 
            self.frame.size.height); 
      self.facadeView.frame = CGRectMake(self.frame.origin.x, 
               self.frame.origin.y, 
               self.frame.size.width, 
               self.frame.size.height); 
     }]; 
    }]; 
} 

- (CGFloat)pickerHeight { 
    CGFloat const kPickerHeight = 216; 
    return kPickerHeight; 
} 

-(CGFloat)navBarHeight { 
    CGFloat const kNavBarHeight = 44; 
    return kNavBarHeight; 
} 

-(NSTimeInterval)animationInterval { 
    NSTimeInterval const kAnimationInterval = 0.2; 
    return kAnimationInterval; 
} 

@end 

... 

// 
// DataPickerPresenter.h 
// ShowDatePickerView 
// 
// Created by Christopher John Morris on 10/11/13. 
// Copyright (c) 2013 Christopher John Morris. All rights reserved. 
// 

#import "PickerPresenter.h" 

@protocol DataPickerPresenterDelegate; 

@interface UIPickerViewPresenter : PickerPresenter 

/* 
* Singleton instance of class 
*/ 
+ (id) defaultPresenter; 

/* 
* Calls back to register button clicks 
*/ 
@property (nonatomic, strong) id<DataPickerPresenterDelegate>delegate; 

- (void) setDataArrayWithArray:(NSArray *)array; 

@end 

/* 
* Delegate that is used to send messages from subclasses 
* to classes using the subclass. 
*/ 
@protocol DataPickerPresenterDelegate 

@required 
- (void) dataPickerPresenerClickedCancel:(UIPickerViewPresenter *)picker; 
- (void) dataPickerPresenerClickedDone:(UIPickerViewPresenter *)picker withString:(NSString *)string index:(NSInteger)index; 

@end 

... 

// 
// DataPickerPresenter.m 
// ShowDatePickerView 
// 
// Created by Christopher John Morris on 10/11/13. 
// Copyright (c) 2013 Christopher John Morris. All rights reserved. 
// 

#import "UIPickerViewPresenter.h" 

@interface UIPickerViewPresenter() <PickerPresenterButtonDelegate, UIPickerViewDelegate, UIPickerViewDataSource> 

@property (nonatomic, strong) UIPickerView *picker; 
@property (nonatomic, strong) NSArray *pickerData; 

@end 

@implementation UIPickerViewPresenter 

+(id)defaultPresenter { 
    static UIPickerViewPresenter *defaultPresenter = nil; 
    if (!defaultPresenter) { 
     static dispatch_once_t onceToken; 
     dispatch_once(&onceToken, ^{ 
      defaultPresenter = [[super allocWithZone:NULL] init]; 
     }); 
    } 
    return defaultPresenter; 
} 

+ (id)allocWithZone:(NSZone *)zone { 
    return [self defaultPresenter]; 
} 

- (id)copyWithZone:(NSZone *)zone { 
    return self; 
} 

- (id)init { 
    self = [super init]; 
    if (self) { 
     [self initializePicker]; 
     buttonDelegate = self; 
    } 
    return self; 
} 

- (void) initializePicker { 
    self.picker = [[UIPickerView alloc] init]; 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGRect frame = self.picker.frame; 
    frame.origin.y = frame.origin.y + self.navBarHeight; 
    frame.size.width = screenRect.size.width; 
    self.picker.frame = frame; 
    self.picker.delegate = self; 
    self.picker.dataSource = self; 
    [pickerContainer addSubview:self.picker]; 
} 

-(void)didCancel { 
    if (self.hideOnCancelClicked) { 
     [self hide]; 
    } 
    [self.delegate dataPickerPresenerClickedCancel:self]; 
} 

-(void)didSave { 
    if (self.hideOnDoneClicked) { 
     [self hide]; 
    } 
    NSInteger selectedRow = [self.picker selectedRowInComponent:0]; 
    NSString *selection = [self.pickerData objectAtIndex:selectedRow]; 
    [self.delegate dataPickerPresenerClickedDone:self withString:selection index:selectedRow]; 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

} 

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
    return 1; 
} 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    return self.pickerData.count; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    return [self.pickerData objectAtIndex:row]; 
} 

- (void)setDataArrayWithArray:(NSArray *)array { 
    self.pickerData = array; 
    [self.picker reloadAllComponents]; 
} 

@end 

對其進行初始化,寫:

UIPickerViewPresenter *presenter = [UIPickerViewPresenter defaultPresenter]; 
[presenter setDataArrayWithArray:self.dataArray]; 

其中 'self.dataArray' 是你想用填充UIPickerView數據。要顯示它,請致電:

[presenter show]; 
+0

您好,感謝您的回覆,你能以任何機會點我到哪裏,我可以看到做一個在一個UIView的樣品,並調整位置和大小的一個方向?謝謝 – user3001526