2012-03-19 56 views
0

当我在界面生成器中制作一个具有背景的自定义UIButton时,它给了我内置触摸时的默认功能,它将图像变黑。 如果我创建的UIButton编程,就像这样:UIButton IB vs编程方式

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

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

它不会表现得像我在IB创建了所有的人。当我接触到内部时,没有什么变化。 在我失踪的UIButton中有设置吗? 感谢

回答

1

我想你必须启用这两个选项之一:

buttonPic.showsTouchWhenHighlighted = YES; 
buttonPic.reversesTitleShadowWhenHighlighted = YES; 
4

这是如何以编程方式创建一个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];