ios - 添加背景时按钮被禁用

标签 ios uiview uiviewcontroller

我正在使用 Parse anypic 教程,我想创建一个不同的 UI。但是我有一些麻烦。

所以,我的 ViewController 是这样的:

@interface PAPHomeViewController ()
@property (nonatomic, strong) PAPSettingsActionSheetDelegate *settingsActionSheetDelegate;
@property (nonatomic, strong) UIView *blankTimelineView;
@end

@implementation PAPHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"LoadView is called");

    // Present Anypic UI
    [self presentUI];

}

-(void) presentUI {

/*UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        [backgroundImageView setImage:[UIImage imageNamed:@"DefaultAnypic.png"]];
        self.view = backgroundImageView;*/

    // Settings button
    self.settingsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.settingsButton addTarget:self
               action:@selector(settingsButtonAction:)
     forControlEvents:UIControlEventTouchUpInside];
    [self.settingsButton setTitle:@"Settings" forState:UIControlStateNormal];
    self.settingsButton.frame = CGRectMake(200.0, 200.0, 100.0, 100.0);
    [self.view addSubview:self.settingsButton];
}

它的 .h 文件是这样的:

@interface PAPHomeViewController : PAPPhotoTimelineViewController

@end

PAPPhotoTimelineViewController 也是一个单独的 ViewController,Home 从它扩展,它是一个 tableViewController,它也调用 ViewDidLoad。

问题:

通过上面的代码,我看到了我的按钮,我可以点击我的按钮。

但是,如果我取消对背景的注释,我确实看到了背景,我确实看到了按钮,但无法点击 - 就像没有识别到​​按钮上的触摸一样。

我也很困惑,现在我正在扩展另一个也实现了 viewDidLoad 的 ViewController,为什么它们都被调用,以什么顺序等。

最佳答案

你不应该为你的 self.view 分配一个新的 UIView

而不是 self.view = backgroundImageView;,就像添加一个随机 View 一样。

[self.view addSubview:backgroundImageView];

这样做,您将按照正确的方式添加 subview :您的 backgroundImageView 将显示在您的 View 中,您的按钮将添加到它上面。

关于ios - 添加背景时按钮被禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26691740/

相关文章:

ios - 如何快速调整导航项目栏中的图像图标?

ios - 如何在 xamarin ios native 中将所有屏幕尺寸的图像水平居中

ios - 运行时错误 - "Nested Push animation"调用 popToRootViewController 时

ios - 永久删除行(或数组项) - swift 3/4

iphone - 关于 iOS 应用程序的复杂设计问题

ios - 新的 UIView 阻止 UITapGesture

ios - 用键盘移动屏幕

iphone - 使用完成 block 呈现和关闭 UIViewController - 没有协议(protocol)和委托(delegate)

ios - 如何用单个表格部分显示不同的 View ?

ios - UIView overrideUserInterfaceStyle 不起作用