iPhone:在弹出的DatePicker框架内添加完成按钮 [英] iPhone: Adding a Done button within a pop up DatePicker frame

查看:122
本文介绍了iPhone:在弹出的DatePicker框架内添加完成按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我弹出一个DatePicker与以下。现在我正在弹出框架顶部添加一个完成按钮。

I pop up a DatePicker with the following. Now I'm trying to add a Done button at the top of the pop up frame.

-(IBAction) contactBDayDatePicker{

NSLog(@"contactBDayDatePicker");

pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;

if (self.pickerView.superview == nil){

    [self.view.window addSubview: self.pickerView];

    // size up the picker view to our screen and compute the start/end frame origin for our slide up animation
    //
    // compute the start frame
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];

    NSLog(@"screenRect %@",NSStringFromCGRect(screenRect));

    CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];

    NSLog(@"pickerSize %@",NSStringFromCGSize(pickerSize));

    CGRect startRect = CGRectMake(0.0,
                                  screenRect.origin.y + screenRect.size.height,
                                  pickerSize.width, pickerSize.height);
    self.pickerView.frame = startRect;

    NSLog(@"pickerView.frame %@",NSStringFromCGRect(startRect));

    // compute the end frame
    CGRect pickerRect = CGRectMake(0.0,
                                   screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                   pickerSize.width,
                                   pickerSize.height+10);

    NSLog(@"pickerRect %@",NSStringFromCGRect(pickerRect));

    // start the slide up animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    // we need to perform some post operations after the animation is complete
    [UIView setAnimationDelegate:self];

    self.pickerView.frame = pickerRect;

    [UIView commitAnimations];

}

 } 

将框架大小增加10点(参见上面的CGSize pickerSize),并认为我可以使用以下内容,但按钮拒绝显示,加上我不知道如何将按钮放置在pickerSize框架本身。 (BTW)如果重要的是DatePicker和Button覆盖scrollView。

I have tried increasing the frame size by 10 points (see CGSize pickerSize above) and thought I could use something like the following, but the button refuses to display, plus I'm not sure how to place the button inside the pickerSize frame itself. (BTW) The DatePicker and Button are overlaying a scrollView if that matters.

  UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [button setTitle:@"Done" forState:UIControlStateNormal];
  button.center = CGPointMake(160,240);
  [button addTarget:self action:@selector(done)    forControlEvents:(UIControlEventTouchUpInside)];
  [self.view addSubview:button];

赞赏任何想法。

推荐答案

我基本完成了同样的事情。我分类UIView。它动画和一切。以下是一些代码:

I've done basically the exact same thing. I subclassed UIView. It animates up and everything. Here's some code for you:

#import <UIKit/UIKit.h>

#define MyDateTimePickerHeight 260

@interface MyDateTimePicker : UIView {
}

@property (nonatomic, assign, readonly) UIDatePicker *picker;

- (void) setMode: (UIDatePickerMode) mode;
- (void) setHidden: (BOOL) hidden animated: (BOOL) animated;
- (void) addTargetForDoneButton: (id) target action: (SEL) action;

@end


#define MyDateTimePickerPickerHeight 216
#define MyDateTimePickerToolbarHeight 44

@interface MyDateTimePicker() 

@property (nonatomic, assign, readwrite) UIDatePicker *picker;
@property (nonatomic, assign) CGRect originalFrame;

@property (nonatomic, assign) id doneTarget;
@property (nonatomic, assign) SEL doneSelector;

- (void) donePressed;

@end


@implementation MyDateTimePicker

@synthesize picker = _picker;
@synthesize originalFrame = _originalFrame;

@synthesize doneTarget = _doneTarget;
@synthesize doneSelector = _doneSelector;

- (id) initWithFrame: (CGRect) frame {
    if ((self = [super initWithFrame: frame])) {
        self.originalFrame = frame;
        self.backgroundColor = [UIColor clearColor];

        CGFloat width = self.bounds.size.width;
        UIDatePicker *picker = [[[UIDatePicker alloc] initWithFrame: CGRectMake(0, 0, width, MyDateTimePickerPickerHeight)] autorelease];
        [self addSubview: picker];

        UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame: CGRectMake(0, MyDateTimePickerPickerHeight, width, MyDateTimePickerToolbarHeight)] autorelease];
        toolbar.barStyle = UIBarStyleBlackOpaque;

        UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc] initWithTitle: @"Done" style: UIBarButtonItemStyleBordered target: self action: @selector(donePressed)] autorelease];
        doneButton.width = width - 20;
        toolbar.items = [NSArray arrayWithObject: doneButton];
        [self addSubview: toolbar];

        self.picker = picker;
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void) setMode: (UIDatePickerMode) mode {
    self.picker.datePickerMode = mode;
}

- (void) donePressed {
    if (self.doneTarget) {
        [self.doneTarget performSelector: self.doneSelector];
    }
}

- (void) addTargetForDoneButton: (id) target action: (SEL) action {
    self.doneTarget = target;
    self.doneSelector = action;
}

- (void) setHidden: (BOOL) hidden animated: (BOOL) animated {
    CGRect newFrame = self.originalFrame;
    newFrame.origin.y += hidden ? MyDateTimePickerHeight : 0;
    if (animated) {
        [UIView beginAnimations: @"animateDateTimePicker" context: nil];
        [UIView setAnimationDuration: MyConstantsElementAnimationLength];
        [UIView setAnimationCurve: UIViewAnimationCurveEaseOut];

        self.frame = newFrame;      

        [UIView commitAnimations]; 
    } else {
        self.frame = newFrame;      
    }
}

@end

这篇关于iPhone:在弹出的DatePicker框架内添加完成按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆