0

I have just started working as an iOS developer, currently working on a custom carousel feature for iOS apps implemented on iPhones and iPads. I'm facing a orientation issue in iPad, where the carousel get misplaced or disappears when the orientation changes.

Following are the lines of code I have written to do the task

-(void)initWithParentView:(UIView *)parentView{
    //identifying the size of the screen
    CGRect screenSize = [[UIScreen mainScreen]bounds];

    //based on the screen size i'm setting the position of the carousel  
    NavigationCarousel *selfCarousel = [super initWithFrame:CGRectMake(0.0, screenSize.size.height - 110, parentView.bounds.size.width, 55.0)];

    //this is the scroll view which will be added as a subview
    self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 0.0, 260, 55.0)];

    //adding the subview 
    [selfCarousel addSubview: self.contentView];
}

For identifying the orientation change I'm writing the following code in the class that makes call to about method.

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceBatteryLevelDidChangeNotification object:nil];
}

-(void)OrientationChange:(NSNotification*)notification{
    UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation];
    if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight)
    {
        NSLog(@"Landscape");
        [self callTheClass];
    }
    else if(Orientation==UIDeviceOrientationPortrait)
    {
        NSLog(@"Portrait");
        [self callTheClass];
    }
}

-(void)callTheClass{
    self.carousel = [[NavigationCarousel alloc] initWithParentView:self.view];
    self.carousel.delegate = self;
}

When I'm debugging, I found that the screen-size is updated as per the orientation of iPad but the carousel is not shown on the tab. When the app is launched, the carousel appears based on the device orientation and disappears when the orientation changes.

What could be the reason why the iPad view is not getting updated when the orientation is changed?

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
Prashanth Kp
  • 61
  • 1
  • 7
  • It is worth learning about constraints and auto layout. If you use constraints rather than setting the frame directly then rotation will be handled for you – Paulw11 Mar 12 '16 at 12:15

1 Answers1

0

I strongly recommend you to use Auto Layout, it will make your developer life much easier. With Auto Layout you will forget about such problems :) There are a lot of open-source Auto Layout frameworks, which provide reach functionality. My favorite are:

They have great documentation with examples, I hope you will learn them fast.

Misternewb
  • 1,066
  • 9
  • 12
  • Thanks Misternewb, I have started looking into Autolayout and Constrains. But is it possible to apply constrains or autolayout to my view controller without effecting other UI of the project. Because my code is part of a huge project? – Prashanth Kp Mar 12 '16 at 19:29
  • You can start using Autolayout in project, which did not use it before, and everything will work fine. Here is a more detailed answer http://stackoverflow.com/a/13539782/4757335. – Misternewb Mar 12 '16 at 19:37
  • i think this could probably help me out. Thanks. – Prashanth Kp Mar 12 '16 at 19:53