【问题标题】:UIButton not showing up in UIViewControllerUIButton 没有出现在 UIViewController 中
【发布时间】:2014-01-23 23:14:20
【问题描述】:

我的按钮设置如下:

CGRect button1Frame = CGRectMake(200, 200, 100, 100);
self.button1 = [[UIButton alloc] initWithFrame:button1Frame];
[self.button1 addTarget:self action:@selector(goToController1:)
 forControlEvents:UIControlEventTouchDown];
self.button1.userInteractionEnabled = YES;
self.button1.opaque = YES;
self.button1.backgroundColor = [UIColor redColor];
[self.button1 setTitle:@"B1" forState:UIControlStateNormal];
[self.view addSubview:self.button1];

与我的发件人:

- (void) goToController1:(id)sender {
    NSLog(@"Pressed");
}

并且我的视图控制器已正确初始化。 什么会导致我的按钮不显示?我已经检查以确保一切都被正确调用。

【问题讨论】:

  • 不是问题的原因,只是请注意,您通常应该对按钮使用UIControlEventTouchUpInside 控制事件,而不是触摸。这是一种更好的用户体验,因为只有当您在按钮内松开手指时才会执行该操作,并让您有机会取消。
  • 谢谢。我现在就换。希望有人回答这个问题。有很多关于这个的帖子,但似乎没有一个能解决我的问题。
  • 这段代码在你的viewController中吗?你在哪里叫它?
  • 是的,这是在视图控制器中。抱歉没有说清楚。
  • 尝试将其放入 viewWillAppear - self.view 在 vi​​ewDidLoad 中可能仍为 nil。

标签: ios objective-c uiview uibutton


【解决方案1】:

你可以试试这个:

button = [UIButton buttonWithType: UIButtonTypeSystem];

然后像这样设置框架:

button.frame = CGRectMake(0,0,20,20);

UIButton buttonWithType: 方法一次性解决了我的问题。不过,不确定在这种情况下是否会有所作为。只是想尝试一下。

【讨论】:

  • 从 iOS 7 开始,UIButtonTypeRoundedRect 已弃用 - 您应该改用 UIButtonTypeSystemDocumentation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多