iOS - 如何在横向模式下显示UILabel和UIImageView

时间:2013-04-03 06:31:55

标签: iphone ios ipad uiimageview uilabel

我在我的AppDelegate.m文件中创建了一个UIImageView和UILabel来显示标题图像
我的默认方向是横向模式,但是我无法正确显示图像,它显示如下图{{ 0}}

所需的结果应该如下图所示

UIImage to be in landscape mode

我还尝试在我的AppDelegate.m文件中旋转图像和标签这是我的代码
但它不显示图像,标题就会消失。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[LMSFirstViewController alloc] initWithNibName:@"LMSFirstViewController" bundle:nil];
    UIViewController *viewController2 = [[LMSSecondViewController alloc] initWithNibName:@"LMSSecondViewController" bundle:nil];
    UIViewController *viewController3 = [[LMSThirdViewController alloc] initWithNibName:@"LMSThirdViewController" bundle:nil];

    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(50, 0, 5000, 50)];
    title.text=@"Library Management System";
    [title setFont:[UIFont systemFontOfSize:24]];
    [title setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
    title.textColor=[UIColor redColor];
    title.transform=CGAffineTransformMakeRotation(M_PI_2);


    UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5000, 50)];

    imageView.image=[UIImage imageNamed:@"LMS.jpg"];
    imageView.transform=CGAffineTransformMakeRotation(M_PI_2);


    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3,nil];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    [self.window addSubview:imageView];
    [self.window addSubview:title];
    return YES;
}

2 个答案:

答案 0 :(得分:0)

您创建了以纵向模式显示的视图。它显示如下

当应用程序进入横向模式时,会在此过程中调用方法,并使用这些方法重新构建视图。

看看这个question

答案 1 :(得分:0)

感谢大家的回复,

我没有在AppDelegate.m中创建UIImageView和UILabel,而是在我的viewController.m文件中写了它并且它正常工作

-(void)viewDidLoad{
     UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(50, 0, 5000, 50)];
    title.text=@"Library Management System";
    [title setFont:[UIFont systemFontOfSize:24]];
    [title setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
    title.textColor=[UIColor redColor];


    UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5000, 50)];

    imageView.image=[UIImage imageNamed:@"LMS.jpg"];
    [self.view addSubView:imageView];
    [self.view addSubView:title];

}

根据您在iOS 6中的方向自动旋转UIImageView和UILabel

我不知道这种方法是否正确,因为如果你想在你的项目的每个视图中显示某些东西,你应该在你的AppDelegate.m文件中写下来这不是更好的编程你不应该编码在每个视图中你应该在AppDelegate.m文件中实现它,但在我的情况下,我得到了结果。
我将尝试解决它。​​