iOS7 iPhone演示视图控制器-UIPickerView



我有一个视图控制器(vc),上面有一个UIPickerView,它需要从屏幕的下半部分向上滑动。

使用:

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

使页面以全屏显示

我读到把UIPickerViews放在活动表中已经不是一个好主意了,有什么建议吗?

我读到把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];

当您有UITableView时,也许可以使用以下方法:https://github.com/aberger/ABMExpandingTableViewCells

最新更新