Reputation: 5936
Having only really used IB builder before, im having problems resizing my objects in my view.
I have a home screen built in code (not my choice) and im resizing it for ipad. The problem is nothing seems to be happening regarding the resizing.
- (void)loadView
{
// Create the main view
UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mainView.backgroundColor = [UIColor blackColor];
// Add textured background
UIImageView *textureBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 580.0f)];
textureBg.image = [UIImage imageNamed:@"BgTexture"];
[mainView addSubview:textureBg];
//////////////////////////////////Code addded resize view/////////////////////////////////////////
[textureBg setAutoresizingMask:(UIViewAutoresizingFlexibleHeight| UIViewAutoresizingFlexibleWidth)];
// Add clipboard background
UIImageView *clipBg = [[UIImageView alloc] initWithFrame:CGRectMake(7.0f, 6.0f, 305.0f, 465.0f )];
clipBg.image = [UIImage imageNamed:@"BgHomeNew2"];
clipBg.userInteractionEnabled = YES;
////////////////////////////////////Code addded resize view/////////////////////////////////////////
[clipBg setAutoresizingMask:(UIViewAutoresizingFlexibleBottomMargin| UIViewAutoresizingFlexibleWidth)];
// Add about button to clipbg
UIButton *aboutButton = [UIButton buttonWithType:UIButtonTypeCustom];
aboutButton.frame = CGRectMake(240.0f, 90.0f, 31.0f, 33.0f);
[aboutButton setImage:[UIImage imageNamed:@"LabelButton"] forState:UIControlStateNormal];
[aboutButton addTarget:self action:@selector(aboutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[clipBg addSubview:aboutButton];
// Create array of arrays to hold button image names and their targets
NSMutableArray *buttonsArray = [[NSMutableArray alloc] initWithObjects:
[NSArray arrayWithObjects:
@"BtnNewCert", @"newCertButtonPressed:", nil],
[NSArray arrayWithObjects:
@"BtnContractorDetails", @"contractorDetailsButtonPressed:", nil],
[NSArray arrayWithObjects:
@"BtnSavedCerts", @"savedCertsButtonPressed:", nil],
nil];
// Iterate over the array and create the buttons
CGPoint buttonOrigin = CGPointMake(16.0f, 157.0f);
for (NSArray *buttonArray in buttonsArray) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(buttonOrigin.x, buttonOrigin.y, 273.0f, 88.0f);
[button setBackgroundImage:[UIImage imageNamed:[buttonArray objectAtIndex:0]] forState:UIControlStateNormal];
[button addTarget:self action:NSSelectorFromString([buttonArray objectAtIndex:1]) forControlEvents:UIControlEventTouchUpInside];
//[button addTarget:self action:@selector(playButtonSound) forControlEvents:UIControlEventTouchUpInside];
buttonOrigin.y += 98.0f;
[clipBg addSubview:button];
}
[mainView addSubview:clipBg];
// Add more apps button
DebugLog(@"Adding more apps button");
UIButton *moreAppsButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreAppsButton.frame = CGRectMake(25.0f, 12.0f, 75.0f, 24.0f);
[moreAppsButton addTarget:self action:@selector(moreAppsButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[moreAppsButton setImage:[UIImage imageNamed:@"BtnMoreApps"] forState:UIControlStateNormal];
[mainView addSubview:moreAppsButton];
// Add cabinet for iPhone5
UIImageView *cabinet = [[UIImageView alloc] initWithFrame:CGRectMake(-2.0f, 445.0f, 324.0f, 128.0f )];
cabinet.image = [UIImage imageNamed:@"Cabinet2"];
[mainView addSubview:cabinet];
[mainView bringSubviewToFront:cabinet];
self.view = mainView;
Upvotes: 0
Views: 430
Reputation: 2530
This is expected behavior. Nothing needs to be resized. You initialize your main view with the screen bounds. So whether it's on iPhone or iPad it will get the screen size of the device directly and won't be resized until there is a rotation or you programmatically change the frame of the main view. There is no way for iOS to understand that you want your textureBg
to be 320 points on the iPhone and to extend if it's on the iPad.
You need to compute the frame of the views that need to be adapted to screen size.
And you should avoid using fixed size like 320, etc. and prefer things such as self.view.bounds.size.width
. It will save you time in your situation where you need to adapt to iPad.
Upvotes: 1