以编程方式创建的UIButton不可见

时间:2014-07-24 10:41:03

标签: ios objective-c cocoa-touch uibutton uikit

我创建了一个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];
}

4 个答案:

答案 0 :(得分:3)

你应该在crate按钮时使用cluster方法:

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

此代码应该有效。确保正确设置scrollView框架和内容大小,并将scrollView添加到self.view。

buttonWithType:是创建按钮的推荐方法,这是指定按钮类型的唯一方法。 当你使用alloc init时,我相信你使用了一些标准的按钮类型,如果iOS改变了,标准也可以改变。

答案 1 :(得分: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];
}

忘了说你需要添加你的方法而不是任何方法.. 希望它有所帮助。

答案 2 :(得分:1)

当我们以编程方式创建UIButton对象时,UIButton的默认标题颜色为白色。

因此,设置UIButton对象的标题颜色。

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

答案 3 :(得分:1)

iOS 7或更高版本将具有透明按钮。除非你设置文字,否则你将无法看到。请执行以下操作:

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