2014-07-24 51 views
0

我創建了一個UIButton,將它添加到視圖中,然後將其添加到UIScrollView。此視圖還包含顯示並格式正確的UITextView以編程方式創建的UIButton不可見

我的代碼如下。希望有人能幫助。該按鈕所在的區域是可點擊的,並且操作正確,但該按鈕不可見。我甚至將tintColor改爲紅色,但我看不到它。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

UIView *billBlocker = *UIView initialized* 
//The UITextView is a subview of `billBlocker`, as well as the `billBlockButton` 
//added the UITextView here 


UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30)]; 


    [billBlockButton setTitle:@"BillBlockerFrenzy" forState:UIControlStateNormal]; 
    [billBlockButton setTintColor:[UIColor redColor]]; 

    [billBlockButton addTarget:self action:@selector(segueToRegulationsGame) forControlEvents:UIControlEventTouchUpInside]; 




    [billBlocker addSubview:billBlockButton]; 





    [self.scrollView addSubview:billBlocker]; 
} 
+0

我會想念這樣一行:'[self.scrollView addSubview:billBlockButton]',對於你已經忘了澄清'billBlocker',以及它是如何inited。 – holex

+0

你永遠不會用initWithFrame初始化一個UIButton。 UIButton不是從UIView繼承NSControl,所以你沒有initWithFrame –

+0

我可以在這裏問一個有點相關的問題嗎?我不知道我在做什麼錯誤的StackOverflow問題。我不明白爲什麼這個問題被拒絕了。你能解釋我應該怎麼做才能正確地問我的問題嗎? –

回答

3

您應該使用聚類方法,當箱子按鈕:

UIButton *billBlockButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[billBlockButton setFrame:CGRectMake(0, 5,292,30)]; 
... 

此代碼應工作。確保scrollView框架和內容大小設置正確,並將scrollView添加到self.view中。

buttonWithType:是建立按鈕的推薦方法,這是您可以指定按鈕類型的唯一方法。 當你使用alloc init時,我相信你會使用一些標準的按鈕類型,如果iOS更改標準也可能會改變。

+1

謝謝,修復它。你能解釋一下爲什麼?我不明白爲什麼我的方法不起作用。 –

+1

如果修復,請接受它。 – iBhavin

+1

我猜你的方法確實有效,但你看不到按鈕。與上面唯一的區別是,這段代碼指定了四捨五入的rect按鈕 - 它有一個輪廓。你可以嘗試你的原始代碼,將按鈕背景顏色設置爲紅色 - 這可能會顯示它 – CharlesA

2

也許問題是你忘了我嘗試滾動瀏覽的設置插座,其工作像這樣

@property (strong,nonatomic) IBOutlet UIScrollView *sView; //scrollviews outlet 
@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 



//added the UITextView here 


UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30)]; 


[billBlockButton setTitle:@"BillBlockerFrenzy" forState:UIControlStateNormal]; 
[billBlockButton setBackgroundColor:[UIColor redColor]]; 

[billBlockButton addTarget:self action:@selector(nothing)  forControlEvents:UIControlEventTouchUpInside]; 



[self.sView addSubview:billBlockButton]; 
} 

忘了說了,你需要添加你的方法,而不是什麼方法.. 希望它能幫助。

1

當我們以編程方式創建UIButton對象時,UIButton的默認標題顏色是白色。

所以,設置UIButton對象的標題顏色。

[billBlockButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 
+0

華達蘭哇! – NSPratik

0

iOS 7或更高版本將具有透明按鈕。除非你設置文字,否則你將無法看到。請執行以下操作:

[[yourButton layer] setBorderColor:[UIColor blackColor].CGColor]; 
[[yourButton layer] setBorderWidth:2.0]; 
[yourButton setTitle:@"Hello" forState:UIControlStateNormal]; 
相關問題