2012-06-19 2 views
2

У меня есть этот код, и я хочу, чтобы моя кнопка как квадрат,, а также под панелью навигации, вы бы Plase помочь мнекак рисовать кнопку площади в прошивкой под навигационной панели

Спасибо заранее!

Я хочу, чтобы моя кнопка в виде квадрата

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 



int rows = 13, columns = 4; 
UIView *buttonView = [[UIView 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:UIButtonTypeRoundedRect]; 
     button.frame = CGRectMake(70 * x, 28 * y, 70, 28); 

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

} 


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

Edit 2

моя ошибка, когда я с помощью границы

[button.layer setBorderWidth:1.0]; 
    [button.layer setBorderColor:[UIColor blackColor]]; 

enter image description here

ответ

0

Вы устанавливаете свою кнопку размером 70x28, поэтому, если вы хотите кнопку 70x70, вам нужно изменить button.frame = CGRectMake (70 * x, 70 * y, 70, 70), таким образом у вас будут квадратные кнопки.

Кроме того, вы уверены, что это NavigationBar или просто панель инструментов? Если вы используете NavigationController, который использует navigationBar, собственный SDK от Apple не позволит вам легко нарисовать его.

В любом случае в вашем случае просто начните рисовать свои кнопки с y = 44, и у вас не будет кнопок, перекрывающих панель инструментов.

+1

Также не забудьте использовать UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom]; вместо UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect] ;. RoundedRect будет вокруг углов вашей кнопки, в то время как Custom сохранит углы острыми. – Jeremy1026

+0

yeah @ Jeremy1026 верен, если вы используете кнопку по умолчанию, вы получите кнопку с закругленными углами с помощью UIButtonTypeCustom, вы можете делать все, что захотите ... – Ladislav

+0

@ Jeremy1026 благодарит за ответ! когда я использую UIButtonTypeCustom, тогда у меня нет границы, как я могу установить borde? –

0

Попробуйте

int rows = 13, columns = 4; 
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f,44, 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(75 * x, 75 * y, 70, 70); 
     button.backgroundColor=[UIColor redColor]; 
     [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]; 

Для использования слоя свойства, которое нужно добавить QuartzCore.framework в вашем проекте.

+0

спасибо, что это было прекрасно, знаете ли вы, что мне делать, если я хочу добавить число для каждого квадрата от 1 до 52 ..? в центре каждого квадрата? –

0

Определит целочисленные переменный и установить это как название кнопки

int rows = 13, columns = 4; 
int number=1; 
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f,44, 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(75 * x, 75 * y, 70, 70); 
     button.backgroundColor=[UIColor redColor]; 
     [button setTitle:[NSString stringWithFormat:@"%d",number] forState:UIControlStateNormal]; 

     button.tag=number; 
     [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [buttonView addSubview: button]; 
     number++; 
    } 
} 

Надеется, что это поможет вам перейти к следующему экрану. Button.tag дает точную кнопку, которую вы выбрали.

Смежные вопросы