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

Posted

技术标签:

【中文标题】以编程方式创建的 UIButton 不可见【英文标题】:Programmatically created UIButton not visible 【发布时间】:2014-07-24 10:41:03 【问题描述】:

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

【问题讨论】:

我会错过这样一行:[self.scrollView addSubview:billBlockButton],因为你忘记了澄清billBlocker 是什么以及它是如何启动的。 你永远不会用 initWithFrame 初始化 UIButton。 UIButton 继承自 NSControl 而不是 UIView,所以你没有 initWithFrame 我能在这里问一个有点相关的问题吗?我不知道我在 *** 问题上做错了什么。我不明白为什么这个问题被否决了。你能解释一下我应该怎么做才能正确地问我的问题吗? 【参考方案1】:

在 crate 按钮时应该使用集群方法:

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

这段代码应该可以工作。确保 scrollView 框架和内容大小设置正确,并将 scrollView 添加到 self.view。

buttonWithType:推荐用于创建按钮的方法,这是您指定按钮类型的唯一方法。 当您使用 alloc init 时,我相信您会使用一些标准按钮类型,如果 ios 更改标准也可能会更改。

【讨论】:

谢谢你,解决了。你能解释一下为什么吗?我不明白为什么我的方法不起作用。 我猜你的方法确实有效,但你看不到按钮。与上面的唯一区别是这段代码指定了圆角矩形按钮 - 它有一个轮廓。您可以尝试将按钮背景颜色设置为红色来尝试原始代码 - 这可能会显示它 @iBhavin 我不得不等待一段时间,它不允许我接受答案【参考方案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];

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

【讨论】:

【参考方案3】:

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

所以,设置 UIButton 对象的标题颜色。

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

【讨论】:

【参考方案4】:

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

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

【讨论】:

以上是关于以编程方式创建的 UIButton 不可见的主要内容,如果未能解决你的问题,请参考以下文章

快速以编程方式创建后如何在情节提要中链接 UIButton

iOS 以编程方式创建的按钮不起作用

无法隐藏以编程方式创建的 UIButton

点击 UIButton 以编程方式创建另一个 UIButton

如何在 UITableView Cell 中以编程方式创建 n 个 UIButton?

更改以编程方式创建的单击 UIButton 的背景图像