Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式创建的UIButton不可见_Ios_Objective C_Cocoa Touch_Uibutton_Uikit - Fatal编程技术网

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

Ios 以编程方式创建的UIButton不可见,ios,objective-c,cocoa-touch,uibutton,uikit,Ios,Objective C,Cocoa Touch,Uibutton,Uikit,我创建了一个UIButton,将其添加到视图中,然后将其添加到UIScrollView中。此视图还包含一个UITextView,该视图显示正确且格式正确 我的代码如下。希望有人能帮忙。按钮应位于的区域是可单击的,并且操作正确,但是按钮不可见。我甚至把颜色改成了红色,但我看不见 - (void)viewDidLoad { [super viewDidLoad]; UIView *billBlocker = *UIView initialized* //The UITextView is

我创建了一个UIButton,将其添加到视图中,然后将其添加到UIScrollView中。此视图还包含一个UITextView,该视图显示正确且格式正确

我的代码如下。希望有人能帮忙。按钮应位于的区域是可单击的,并且操作正确,但是按钮不可见。我甚至把颜色改成了红色,但我看不见

- (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];
}
忘了说需要添加方法而不是nothing方法。。
希望有帮助。

当我们以编程方式创建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];

我会错过这样一句话:[self.scrollView addSubview:billBlockButton],关于你已经忘记了澄清什么是billBlocker以及它是如何初始化的。你从来没有用initWithFrame初始化过UIButton。UIButton继承自NSControl,而不是UIView,因此您没有InitWithFrame我可以在这里问一个相关的问题吗?我不知道我在这些问题上做错了什么。我不明白为什么这个问题被否决了。你能解释一下我应该怎么做才能正确地提问吗?谢谢你,这就解决了问题。你能解释一下原因吗?我不明白为什么我的方法不起作用。我猜你的方法起作用了,但是你看不到按钮。与上面的唯一区别是,这段代码指定了圆角rect按钮,它有一个轮廓。您可以尝试将按钮背景颜色设置为红色,这可能会显示您的原始代码it@iBhavin我不得不等了一段时间,它不允许我接受答案