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?