2012-03-19 63 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];