0

I want to create view with 52 square or button (for 52 week in a year in one page) with grid but I don't know how should I align them or how should I put them in a frame

(you will have 13 rows and 4 columns), but if you try this code it's not algin : I don't know how should I create frame to put all of buttons in side of my frame..

Here is my code:

- (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));
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 1
    I tried your code, and buttons are aligned... Can you post a picture of what you've got so far, and detail what you really want to obtain ? – NSZombie Jun 19 '12 at 09:43
  • @NSZombie thanks for reply I put the picture –  Jun 19 '12 at 09:49

3 Answers3

4

Instead of adding your buttons directly to your controller's view, create a subview which will contain all your buttons. Then, center this subview. Here is the code you could use :

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];

If you want your buttons to occupy the whole view, play with the width and height of your buttons (you used 58 and 31).

NSZombie
  • 1,857
  • 14
  • 14
  • Thanks, do you know how can I use square button?and also how can I have my button under navigation, year view? thanks alot! –  Jun 19 '12 at 10:02
  • For square buttons, you should use images, it's the quicker way. Or you could also draw them with CoreGraphics, but that's more difficult if you don't know CoreGraphics. And for you year bar, you should use a UINavigationController to display your controller. – NSZombie Jun 19 '12 at 10:04
  • Thanks, I try it but I don't know how tu use image , should i do somthing like this --->>>> UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0.f, 70*columns, 70*rows)]; would you please help me:) Thanks in advance! –  Jun 19 '12 at 11:59
  • You have to associate your button with a UIImage. Like this : [button setImage:myImage forState:UIControlStateNormal] where myImage is a UIImage you created earlier. And your button needs to be of type UIButtonTypeCustom. Cheers. – NSZombie Jun 19 '12 at 12:07
  • Thanks, is it OK ? int rows = 13, columns = 4; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0.f, 70*columns, 70*rows)]; for (int y = 0; y < rows; y++) { for (int x = 0; x < columns; x++) { UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(70 * x, 28 * y, 70, 28); [button addTarget:self action:@selector(buttonPressed:) then how can I address image? Thanks alot! –  Jun 19 '12 at 12:31
  • when I add this it's just show me a big picture UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0.f, 70*columns, 70*rows)]; // [imgView setImage:[UIImage imageNamed:@"icon-s.png"]]; –  Jun 19 '12 at 12:59
1

Can you use this :

CGPoint center = CGPointMake([self.view bounds].size.width/2.0, [self.view bounds].size.height/2.0);    
[button setCenter:center];
ant
  • 22,634
  • 36
  • 132
  • 182
0

Write this code in AppDelegate.m file. Just check it. I run this program it works fine below navigation bar.

- (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;
}
Prasad G
  • 6,702
  • 7
  • 42
  • 65