0

52個の正方形やボタン(1ページに1年で52週分)のビューをグリッドで作成したいのですが、どのように並べたらよいか、どのように枠に収めればよいかわかりません。

(13 行と 4 列になります)、しかし、このコードを試してみると、algin ではありません: すべてのボタンをフレームの横に配置するフレームを作成する方法がわかりません..

これが私のコードです:

- (void)viewDidLoad
{
[super viewDidLoad];
int rows = 13, columns = 4;

for (int y = 0; y < rows; y++) {
    for (int x = 0; x < columns; x++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(58 * x, 31 * y, 58, 31);

        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview: button];

    }
}



}


 -(void)buttonPressed:(UIButton *)button
{
NSLog(@"button %u -- frame: %@", button.tag, NSStringFromCGRect(button.frame));
}
4

3 に答える 3

4

ボタンをコントローラーのビューに直接追加する代わりに、すべてのボタンを含むサブビューを作成します。次に、このサブビューを中央に配置します。使用できるコードは次のとおりです。

int rows = 13, columns = 4;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 58*columns, 58*rows)];
for (int y = 0; y < rows; y++) {
    for (int x = 0; x < columns; x++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(58 * x, 31 * y, 58, 31);

        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [buttonView addSubview: button];

    }
}

// Center the view which contains your buttons
CGPoint centerPoint = buttonView.center;
centerPoint.x = self.view.center.x;
buttonView.center = centerPoint;
[self.view addSubview:buttonView];

ボタンをビュー全体に表示する場合は、ボタンの幅と高さを調整します (58 と 31 を使用しました)。

于 2012-06-19T09:59:39.887 に答える
1

これを使用できますか:

CGPoint center = CGPointMake([self.view bounds].size.width/2.0, [self.view bounds].size.height/2.0);    
[button setCenter:center];
于 2012-06-19T09:56:23.507 に答える
0

このコードをファイルに記述しAppDelegate.mます。確認してください。このプログラムを実行すると、ナビゲーション バーの下で正常に動作します。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

    navigationController.navigationBar.barStyle = UIBarStyleDefault; 
    //navigationController.navigationBar.hidden = YES;
    //navigationController.navigationBar.frame = CGRectMake(0, 20, 320, 40);
   // self.window.rootViewController = self.viewController;
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    return YES;
}
于 2012-06-19T10:10:40.073 に答える