gpt4 book ai didi

iPhone:在弹出的 DatePicker 框架中添加“完成”按钮

转载 作者:行者123 更新时间:2023-12-03 19:02:39 24 4
gpt4 key购买 nike

我弹出一个 DatePicker,其中包含以下内容。现在我尝试在弹出框架的顶部添加一个“完成”按钮。

-(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 内框架本身。 (顺便说一句)如果重要的话,DatePicker 和 Button 会覆盖 ScrollView 。

  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 进行了子类化。它让一切都充满了活力。这里有一些代码供您引用:

#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 框架中添加“完成”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6231587/

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