gpt4 book ai didi

iOS7-iPhone 呈现 View Controller - UIPickerView

转载 作者:行者123 更新时间:2023-11-29 12:58:17 25 4
gpt4 key购买 nike

我有一个带有 UIPickerView 的 View Controller (vc),它需要从屏幕的下半部分向上滑动。

使用:

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

使页面全屏显示

我读到将 UIPickerViews 放在事件表中不再是一个好主意,有什么建议吗?

最佳答案

Ive read that putting UIPickerViews in activity sheets is not a good idea anymore, any suggestions?

老实说,这从来都不是一个好主意,但它成为了一种地下标准。如果在用户将文本字段设置为 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];

关于iOS7-iPhone 呈现 View Controller - UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20457265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com