我有一个带有UIPickerView的视图控制器(vc),它需要从屏幕的下半部分向上滑动。
使用:
ViewControlelr *vc = [self.storyboard instantiateViewControllerWithIdentifier: @"VC"];
[self presentViewController:vc animated:YES completion:nil]
使页面显示为fullscreeen
我已经读过将UIPickerViews放在活动表中不再是一个好主意,有什么建议吗?
答案 0 :(得分: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];
答案 1 :(得分:0)
当你有一个UITableView
时,你可以使用它:https://github.com/aberger/ABMExpandingTableViewCells