0

之前只真正使用过 IB 构建器,我在调整对象大小时遇到​​了问题。

我有一个内置代码的主屏幕(不是我的选择),我正在为 ipad 调整它的大小。问题是关于调整大小似乎没有发生任何事情。

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

1 回答 1

1

这是预期的行为。什么都不需要调整大小。您使用屏幕边界初始化主视图。因此,无论是在 iPhone 还是 iPad 上,它都会直接获取设备的屏幕尺寸,并且在旋转或您以编程方式更改主视图的框架之前不会调整其尺寸。iOS 无法理解您希望textureBg在 iPhone 上达到 320 点,如果在 iPad 上则延长。
您需要计算需要适应屏幕尺寸的视图的框架。并且您应该避免使用像 320 等固定大小,而更喜欢诸如self.view.bounds.size.width. 在您需要适应 iPad 的情况下,它将节省您的时间。

于 2012-09-30T23:05:33.743 回答