UIButton IB против программного - PullRequest
0 голосов
/ 19 марта 2012

Когда я создаю пользовательский UIButton с фоном в конструкторе интерфейсов, он дает мне эту функциональность по умолчанию, когда я подправляю внутри, он как бы затемняет изображение.Если я создаю UIButton программно, например:

    buttonPic = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 63)];
    [[buttonPic imageView] setContentMode:UIViewContentModeScaleToFill];

[buttonPic setImage:[video pic] forState:UIControlStateNormal];

, он не будет вести себя так, как тот, который я создал в IB вообще.Ничего не меняется, когда я подправляю изнутри.Есть ли в UIButton настройки, которые мне не хватает?Спасибо

Ответы [ 2 ]

4 голосов
/ 19 марта 2012

Это как создать UIButton программно.

//Specify the type of the button first. No need to use alloc-init for UIButton like other view objects.

UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 

// Give UIButtonTypeRoundedRect to get default Interface builder style button. 

myBtn.frame = CGRectMake(0, 0, 100, 40);
[self.view addSubview:myBtn];

 // Since the button type is custom, give an image to make it visible.
[myBtn setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:normal];
[myBtn addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];

// These are some optional methods you may want to use.

myBtn.titleLabel.font = [UIFont fontWithName:@"Arial" size:15];
[myBtn setTitle:@"my button" forState:UIControlStateNormal];
[myBtn setTitleColor:[UIColor blackColor] forState:normal];
[myBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
1 голос
/ 19 марта 2012

Я думаю, вы должны включить один из следующих двух вариантов:

buttonPic.showsTouchWhenHighlighted = YES;
buttonPic.reversesTitleShadowWhenHighlighted = YES;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...