简体   繁体   中英

iOS - How to display UILabel and UIImageView in landscape mode

I have created an UIImageView and UILabel in my AppDelegate.m file to display the title image
My default orientation is in landscape mode but i am not able to display the image properly it shows like the below image UIImage处于横向模式

The desired result should be like this image given below

在此处输入图片说明

i also tried to rotate the image and label in my AppDelegate.m file this is my code
But it dosen't display the image and title is just disappears.

 - (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;
}

You created the view to show in the portrait mode.Hence it shows like this

When the app goes to landscape mode there are methods invoked in this process and use these methods for the reframing the views.

Take a look at this question

Thanks Everyone for your response,

Instead of creating a UIImageView and UILabel in AppDelegate.m
i had written it in my viewController.m file and it works properly

-(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];

}

It automatically rotates the UIImageView and UILabel as per your orientation in iOS 6

I dont know this approach is right or not because if you want something to display in your every view of your project you should write it in you AppDelegate.m file
It not better programming you should not code it in each and every view you should implement it in AppDelegate.m file but in my case i am getting the result.
And i will try to solve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM