强制 UIAlertView 进入横向模式

发布于 2025-01-03 05:55:32 字数 270 浏览 4 评论 0原文

我需要以横向模式显示 UIAlertView。我尝试了显而易见的方法,在 willPresentAlertView: 委托方法中设置转换,但无济于事:

-(void) willPresentAlertView:(UIAlertView *)alertView {

    alertView.transform = CGAffineTransformMakeRotation(M_PI_2);
}

有关如何解决此问题的任何建议吗?

I need to display a UIAlertView in landscape mode. I tried the obvious, setting the transform in the willPresentAlertView: delegate method to no avail:

-(void) willPresentAlertView:(UIAlertView *)alertView {

    alertView.transform = CGAffineTransformMakeRotation(M_PI_2);
}

Any suggestions on how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

真心难拥有 2025-01-10 05:55:32

您是否在 didPresentAlertView 中尝试过?

- (void)didPresentAlertView:(UIAlertView *)alertView
{
    // UIAlertView in landscape mode
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:0.1];
    alertView.transform = CGAffineTransformRotate(alertView.transform, 3.14159/2);
    [UIView commitAnimations];
}

Have you tried in the didPresentAlertView?

- (void)didPresentAlertView:(UIAlertView *)alertView
{
    // UIAlertView in landscape mode
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:0.1];
    alertView.transform = CGAffineTransformRotate(alertView.transform, 3.14159/2);
    [UIView commitAnimations];
}
短暂陪伴 2025-01-10 05:55:32

如果您使用 UIViewController,它应该会自动旋转。
您是否忘记在 shouldAutorotateToInterfaceOrientation 中返回 YES 以获得所需的方向?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES; /* auto rotate always */
}

It should rotate automatically if you're using UIViewController.
Did you forget to return YES for the desired orientations in shouldAutorotateToInterfaceOrientation?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES; /* auto rotate always */
}
只有一腔孤勇 2025-01-10 05:55:32

显示警报的视图的方向是什么?我遇到了同样的问题,我尝试在横向视图中显示 UIAlertView 但总是以纵向显示。因此,我强制调整状态栏的方向:

[[UIApplication sharedApplication] setStatusBarOrientation:theOrientation];

这对我有用。

What is the orientation of the view that shows the alert? I had the same problem, I tried to show an UIAlertView inside a landscape view but always appeared in portrait orientation. So, I forced the orientation of the status bar:

[[UIApplication sharedApplication] setStatusBarOrientation:theOrientation];

That worked to me.

十年九夏 2025-01-10 05:55:32

在Alert Window出现之前,设置当前窗口显示在顶部。如果不这样做,您可以看到警报窗口的旋转动画。

-(void) willPresentAlertView:(UIAlertView *)alertView {

    [UIView setAnimationsEnabled:NO];

    self.view.window.windowLevel = 2003;

}

旋转警报窗口

-(void)didPresentAlertView:(UIAlertView *)alertView
{

    UIWindow * alertWindow = alertView.window;
    alertWindow.transform = CGAffineTransformMakeRotation(M_PI / 2);
    alertWindow.bounds = CGRectMake(0, 0, SCREEN_HEIGHT,SCREEN_WIDTH);
    alertWindow.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showLandscapeAlertView) userInfo:nil repeats:NO];

}

旋转警报窗口后,将当前窗口向后移动。

-(void)showLandscapeAlertView {

    self.view.window.windowLevel = 0;

    [UIView setAnimationsEnabled:YES];

}

Before the Alert Window appeared , Set the current window display at the top. If not do this ,you can see the alert window's rotate animate.

-(void) willPresentAlertView:(UIAlertView *)alertView {

    [UIView setAnimationsEnabled:NO];

    self.view.window.windowLevel = 2003;

}

Rotate the Alert Window

-(void)didPresentAlertView:(UIAlertView *)alertView
{

    UIWindow * alertWindow = alertView.window;
    alertWindow.transform = CGAffineTransformMakeRotation(M_PI / 2);
    alertWindow.bounds = CGRectMake(0, 0, SCREEN_HEIGHT,SCREEN_WIDTH);
    alertWindow.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showLandscapeAlertView) userInfo:nil repeats:NO];

}

After the Alert window rotated, move the current window back.

-(void)showLandscapeAlertView {

    self.view.window.windowLevel = 0;

    [UIView setAnimationsEnabled:YES];

}
倥絔 2025-01-10 05:55:32

我最近也遇到了同样的问题。对我来说,解决方案是使用 UIAlertController - 涵盖 UIAlertviewUIActionsheet 的旧处理。

  1. 创建新的控制器,该控制器专用于 UIAlertController,您必须在其中重载方法 viewWillAppearviewWillDisappear,如下例所示。

AlertViewController.h

#import <UIKit/UIKit.h>

@interface AlertViewController : UIAlertController 

@end

AlertViewController.m

#import "AlertViewController.h"

@interface AlertViewController ()

@end

@implementation AlertViewController

- (void) viewWillAppear:(BOOL)animated {
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI_2)];
}

- (void) viewWillDisappear:(BOOL)animated {
    [self.view setHidden:YES];
}

...
  1. 实现在需要的地方显示警报视图的方法。

    • (无效)showInfoAlertView {

       AlertViewController *alert = [AlertViewControlleralertControllerWithTitle:@"我的警报"消息:@"这是一个警报。"首选样式:UIAlertControllerStyleAlert];
      
          UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" 样式:UIAlertActionStyleDefault handler:nil];
          [警报添加操作:确定];
          [selfpresentViewController:alertanimated:NOcompletion:nil];
      }
      

I was struggling with quite the same issue lately. For me solution was using UIAlertController - cover older handling of UIAlertview and UIActionsheet.

  1. Create new controller, which is dedicated from UIAlertController, where you must overloaded methods viewWillAppear and viewWillDisappear, like in example bellow.

AlertViewController.h

#import <UIKit/UIKit.h>

@interface AlertViewController : UIAlertController 

@end

AlertViewController.m

#import "AlertViewController.h"

@interface AlertViewController ()

@end

@implementation AlertViewController

- (void) viewWillAppear:(BOOL)animated {
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI_2)];
}

- (void) viewWillDisappear:(BOOL)animated {
    [self.view setHidden:YES];
}

...
  1. implement method for showing alert view where you needed.

    • (void) showInfoAlertView {

          AlertViewController *alert = [AlertViewController alertControllerWithTitle:@"My Alert" message:@"This is an alert." preferredStyle:UIAlertControllerStyleAlert];
      
          UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
          [alert addAction:ok];
          [self presentViewController:alert animated:NO completion:nil];
      }
      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文