簡體   English   中英

以編程方式創建的UIButton不可見

[英]Programmatically created UIButton not visible

我創建了一個UIButton ,將其添加到視圖中,然后將其添加到UIScrollView 該視圖還包含一個UITextView ,它顯示並正確設置了格式。

我的代碼如下。 希望有人可以提供幫助。 該按鈕應單擊的區域是可單擊的,並且操作正確,但是該按鈕不可見。 我什至將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];
}

當使用條板箱按鈕時,您應該使用群集方法:

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

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

buttonWithType:是創建按鈕的推薦方法,這是指定按鈕類型的唯一方法。 我相信,當您使用alloc init時,您會使用某些標准按鈕類型,並且如果iOS更改,則標准也會更改。

可能的問題是您忘記了我嘗試過的滾動視圖的設置出口及其工作方式

@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];
}

忘了說您需要添加方法而不是什么都不要。

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

因此,設置UIButton對象的標題顏色。

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM