gpt4 book ai didi

iphone - showInView 导致剪裁

转载 作者:行者123 更新时间:2023-11-28 20:36:06 25 4
gpt4 key购买 nike

因此,我遵循了有关如何在用户点击 UITextField 时启动 UIDatePicker 而不是键盘的教程。我遇到的问题是,当选择器出现时,它被截断并在控制台中显示此消息:

Presenting action sheet clipped by its superview. Some controls might not respond to touches. On iPhone try -[UIActionSheet showFromTabBar:] or -[UIActionSheet showFromToolbar:] instead of -[UIActionSheet showInView:].

我尝试了其他方法,但它们也不起作用,我也不希望它们起作用。如果有人可以看看下面的代码并提供一些建议,那就太好了。谢谢:)

AddShiftViewController.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Shift.h"

@interface AddShiftsViewController : UIViewController <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *jobTitleField;
@property (weak, nonatomic) IBOutlet UITextField *startDateField;
@property (weak, nonatomic) IBOutlet UITextField *endDateField;

@property (nonatomic, strong) NSDate *startDate;
@property (nonatomic, strong) NSDate *endDate;

@property (nonatomic, strong) UIActionSheet *dateSheet;

- (void)setDate;
- (void)dismissPicker;
- (void)cancelPicker;

- (IBAction)addShift:(id)sender;

@end

AddShiftViewController.m

#import "AddShiftsViewController.h"

@interface AddShiftsViewController ()

@end

@implementation AddShiftsViewController
@synthesize jobTitleField;
@synthesize startDateField;
@synthesize endDateField;
@synthesize startDate, endDate, dateSheet;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

AppDelegate *dataCenter = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (void)viewDidUnload
{
[self setJobTitleField:nil];
[self setStartDateField:nil];
[self setEndDateField:nil];
[self setEndDate:nil];
[self setStartDate:nil];
[self setDateSheet:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)addShift:(id)sender {
Shift *newShift = [[Shift alloc] init];
}

- (void)setDate {
dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

[dateSheet setActionSheetStyle:UIActionSheetStyleDefault];

CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[picker setDatePickerMode:UIDatePickerModeDateAndTime];

[dateSheet addSubview:picker];

UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];

[controlToolBar setBarStyle:UIBarStyleDefault];
[controlToolBar sizeToFit];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"Set" style:UIBarButtonItemStyleDone target:self action:@selector(dismissPicker)];

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelPicker)];

[controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];

[dateSheet addSubview:controlToolBar];

[dateSheet showInView:self.view];

[dateSheet setBounds:CGRectMake(0, 0, 420, 385)];
}

- (void)dismissPicker {
NSArray *listOfViews = [dateSheet subviews];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM d, yyyy at H:mm"];

for (UIView *subView in listOfViews) {
if ([subView isKindOfClass:[UIDatePicker class]]) {
if (self.startDateField.isEditing) {
self.startDate = [(UIDatePicker *)subView date];
[self.startDateField setText:[formatter stringFromDate:self.startDate]];
[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}
else if (self.endDateField.isEditing) {
self.endDate = [(UIDatePicker *)subView date];
[self.endDateField setText:[formatter stringFromDate:self.endDate]];
[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}
}
}
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self setDate];
return NO;
}

- (void)cancelPicker {
[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}

@end

Shifts.h(自定义类)

#import <Foundation/Foundation.h>

@interface Shift : NSObject

@property (nonatomic, strong) NSString *jobTitle;
@property (nonatomic, strong) NSString *employer;
@property (nonatomic, strong) NSDate *startDate;
@property (nonatomic, strong) NSDate *endDate;

@end

编辑

所以,我尝试切换到:

[dateSheet showFromTabBar:self.tabBarController.tabBar];

但它仍然是裁剪。我附上了一张图片来向您展示正在发生的事情。

enter image description here

最佳答案

看起来你可能把这一行的数字弄错了:

[dateSheet setBounds:CGRectMake(0, 0, 420, 385)];

应该是:

[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];

iPhone 屏幕的宽度不是 420 像素 :) 经典屏幕在 Retina 显示屏上是 320 宽(纵向)或 640 宽。虽然其他地方也可能有错误,但这个错误对我来说很突出。

关于iphone - showInView 导致剪裁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467740/

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