ビューを作成してサブビューとして追加しようとしましたが、アラートビューのボタンと重なっていますUIAlertView
。アラート ビューのサイズを変更しても、ボタンが下に移動しません。また、別の背景などを設定する必要があります。つまり、カスタム アラート ビューを作成する必要があります。iPhoneプログラミング初心者です。これを行う方法を提供してください。UITextField
UITextView
4 に答える
カスタム アラート ビューを作成することはできません。なぜなら、Apple が私たちに手を加えてほしくないものだと判断したからです。アラート内のテキスト フィールドが 1 つだけで、ストックの背景色を使用できる場合setAlertViewStyle:UIAlertViewStylePlainTextInput
は、テキスト フィールドをアラートに配置するために使用できます。
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[myAlertView show];
ただし、本当にこれらの変更を行いたい場合は、独自の を作成しUIView
、アラート ビューのようにドレスアップする必要があります。大まかな例を次に示します。
- (IBAction)customAlert:(UIButton *)sender
{
UIView *myCustomView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 300)];
[myCustomView setBackgroundColor:[UIColor colorWithRed:0.9f green:0.0f blue:0.0f alpha:0.8f]];
[myCustomView setAlpha:0.0f];
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dismissButton addTarget:self action:@selector(dismissCustomView:) forControlEvents:UIControlEventTouchUpInside];
[dismissButton setTitle:@"Close" forState:UIControlStateNormal];
[dismissButton setFrame:CGRectMake(20, 250, 240, 40)];
[myCustomView addSubview:dismissButton];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 240, 35)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[myCustomView addSubview:textField];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 75, 240, 150)];
[myCustomView addSubview:textView];
[self.view addSubview:myCustomView];
[UIView animateWithDuration:0.2f animations:^{
[myCustomView setAlpha:1.0f];
}];
}
- (void)dismissCustomView:(UIButton *)sender
{
[UIView animateWithDuration:0.2f animations:^{
[sender.superview setAlpha:0.0f];
}completion:^(BOOL done){
[sender.superview removeFromSuperview];
}];
}
それを行う最善の方法は、 のサブクラスであるクラスを作成することですがUIAlertView
、UIAlertView
クラス リファレンスには、UIAlertView クラスはそのまま使用することを意図しており、サブクラス化はサポートされていません。このクラスのビュー階層は非公開であり、変更してはなりません。
ポップアップログインを作成する必要があったので、このチュートリアルに従いました。それは非常に便利ですUIAlertView
. 興味深い部分は、当時、YouTube アプリが のサブクラスを使用しているように見えるポップアップ ログイン コントロールを使用していたことですUIAlertView
が、おそらくゼロから作成しUIView
、やってみる価値はあると思います。