iOS7-iPhone呈现视图控制器-UIPickerView

用户名

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

使用:

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的大小是固定的,因此您可能无法控制如何呈现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];

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

UIPickerView的自定义视图在iOS7中损坏

来自分类Dev

ios7呈现视图控制器在呈现时不会旋转,导致状态栏消失

来自分类Dev

iOS8 iOS7和iOS6防止旋转呈现的视图控制器

来自分类Dev

在iOS 7中重用UIPickerView中的视图

来自分类Dev

在iOS 7中重用UIPickerView中的视图

来自分类Dev

iPad / iOS7:从中呈现“全屏”视图控制器后,“页面”模式视图控制器出现奇怪行为

来自分类Dev

iOS - 从呈现的视图控制器呈现视图控制器会改变其高度

来自分类Dev

UIPickerView选择行不适用于iOS7

来自分类Dev

iOS7 / IOS8在视图控制器中仅允许纵向

来自分类Dev

iOS7 / IOS8在视图控制器中仅允许纵向

来自分类Dev

iOS 8更改呈现/模态视图控制器的大小

来自分类Dev

iOS 8更改呈现/模态视图控制器的大小

来自分类Dev

IOS7的幻灯片视图控制器菜单和状态栏问题

来自分类Dev

在iOS7中处理一个视图控制器的自动旋转

来自分类Dev

当用户点击通知IOS7时导航到特定的视图控制器

来自分类Dev

如何在情节提要iOS7中正确重用模式视图/控制器

来自分类Dev

使用情节提要IOS7将UIview控制器显示为子视图

来自分类Dev

模拟iOS7控制器推送动画

来自分类Dev

ios7标签栏控制器的问题

来自分类Dev

如何在导航控制器层次结构中限制iOS7中每个视图控制器的方向

来自分类Dev

iOS 7中的UIPickerView按钮选择器问题

来自分类Dev

在iOS 13的iPhone上将人像View控制器推入横向视图控制器

来自分类Dev

呈现的视图控制器涵盖了iOS 7中的状态栏

来自分类Dev

iOS 7-在呈现模态视图控制器时获取警告消息

来自分类Dev

iOS-在iOS7中获取模式控制器的父控制器

来自分类Dev

多个视图控制器-iOS 7

来自分类Dev

导航控制器以编程方式查看控制器无法正常运行IOS7

来自分类Dev

单击屏幕时如何在iOS7中关闭UIPickerView

来自分类Dev

iOS - 关闭在其视图之外触摸的呈现视图控制器

Related 相关文章

  1. 1

    UIPickerView的自定义视图在iOS7中损坏

  2. 2

    ios7呈现视图控制器在呈现时不会旋转,导致状态栏消失

  3. 3

    iOS8 iOS7和iOS6防止旋转呈现的视图控制器

  4. 4

    在iOS 7中重用UIPickerView中的视图

  5. 5

    在iOS 7中重用UIPickerView中的视图

  6. 6

    iPad / iOS7:从中呈现“全屏”视图控制器后,“页面”模式视图控制器出现奇怪行为

  7. 7

    iOS - 从呈现的视图控制器呈现视图控制器会改变其高度

  8. 8

    UIPickerView选择行不适用于iOS7

  9. 9

    iOS7 / IOS8在视图控制器中仅允许纵向

  10. 10

    iOS7 / IOS8在视图控制器中仅允许纵向

  11. 11

    iOS 8更改呈现/模态视图控制器的大小

  12. 12

    iOS 8更改呈现/模态视图控制器的大小

  13. 13

    IOS7的幻灯片视图控制器菜单和状态栏问题

  14. 14

    在iOS7中处理一个视图控制器的自动旋转

  15. 15

    当用户点击通知IOS7时导航到特定的视图控制器

  16. 16

    如何在情节提要iOS7中正确重用模式视图/控制器

  17. 17

    使用情节提要IOS7将UIview控制器显示为子视图

  18. 18

    模拟iOS7控制器推送动画

  19. 19

    ios7标签栏控制器的问题

  20. 20

    如何在导航控制器层次结构中限制iOS7中每个视图控制器的方向

  21. 21

    iOS 7中的UIPickerView按钮选择器问题

  22. 22

    在iOS 13的iPhone上将人像View控制器推入横向视图控制器

  23. 23

    呈现的视图控制器涵盖了iOS 7中的状态栏

  24. 24

    iOS 7-在呈现模态视图控制器时获取警告消息

  25. 25

    iOS-在iOS7中获取模式控制器的父控制器

  26. 26

    多个视图控制器-iOS 7

  27. 27

    导航控制器以编程方式查看控制器无法正常运行IOS7

  28. 28

    单击屏幕时如何在iOS7中关闭UIPickerView

  29. 29

    iOS - 关闭在其视图之外触摸的呈现视图控制器

热门标签

归档