I need to handle orientation (Portrait - Landscape) in my application. Basic layout of my screen is UIView -> UIImageView -> UITableView (Background = ClearColor) (in that z-order) so that it looks like the table has a background image.
I need to do the following:
- Different images in UIImageView in both modes.
- Each cell in the table needs to have 3/4 images laid out side-by-side (3 in portrait and 4 in landscape).
All of this needs to be done in the interface builder (to the maximum possible extent).
What I have tried so far:
- IB does give you the option of Orientation for View. But I couldn't find a way of setting different image for each orientation.
- Following a hierarchy where I derive 2 separate VCs (with 2 separate NIBs) from one single base class. Only problem is this is a Universal build, so I'll have to do this for each view in both iPad and iPhone.
Is there any other better method? If not, is the 2nd option better? Any problems with that approach?
What you trying to achieve is adding different views basing on orientation in the xib,
Short answer is that you cant,
However you could implement 2 Xibs, one xib for each orientation
Please refer to the answer of this question for further explanation
Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?
does this do it for you?
please check out the below code it's supporting also orientation
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CountryCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// NSString *continent = [[self.countries allKeys] objectAtIndex:indexPath.row];
NSString *continent1 = [self tableView:tableView titleForHeaderInSection:indexPath.section];
NSLog(@"Contine............%@", continent1);
int k = [[self.countries valueForKey:continent1] count];
UIScrollView *previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, 250)];
previewScrollView.backgroundColor = [UIColor clearColor];
previewScrollView.pagingEnabled = TRUE;
previewScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[previewScrollView setContentSize:CGSizeMake(250*k, 60.0)];
previewScrollView.showsHorizontalScrollIndicator = YES;
NSLog(@"cell.contentView.frame.size.widt %f",cell.contentView.frame.size.width);
NSLog(@"K %@ %d",continent1, k);
for(int i=0 ; i<k; i++ ){
imageView.image = [UIImage imageNamed:[[self.countries valueForKey:continent1] objectAtIndex:i]];
imageView.contentMode = UIViewContentModeCenter;
[previewScrollView addSubview:imageView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(250.0*i, 10, 200, 250);
NSLog(@"%@", [NSString stringWithFormat:@"%d%d",indexPath.section,i]);
btn.tag = [[NSString stringWithFormat:@"%d%d",indexPath.section,i] intValue];
[btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
[previewScrollView addSubview:btn];
}
[[cell contentView] insertSubview:previewScrollView atIndex:0];
// [cell addSubview:previewScrollView];
}
return cell;
}