【问题标题】:adding subviews in UINavigationBar with buttons doesn't work在 UINavigationBar 中添加带有按钮的子视图不起作用
【发布时间】:2013-01-14 05:17:16
【问题描述】:

我有一个 UINavigationController 子类,在 viewDidLoad 我有以下代码:

self.containerView_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, self.navigationController.navigationBar.frameHeight)];
    [self.containerView_ setBackgroundColor:[UIColor yellowColor]];
    [self.containerView_ setAutoresizingMask:UIViewAutoresizingNone];

    self.profileButton_ = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.profileButton_ addTarget:self action:@selector(showProfileView:) forControlEvents:UIControlEventTouchUpInside];
    [self.profileButton_ setCenterY:self.navigationController.navigationBar.frameHeight/2];
    [self.profileButton_ setImage:[UIImage imageNamed:@"profile_icon.png"] forState:UIControlStateNormal];

    self.socialButton_ = [[UIButton alloc] initWithFrame:CGRectMake(self.profileButton_.frameRight + 10, 0, 30, 30)];
    [self.socialButton_ addTarget:self action:@selector(showSocialView:) forControlEvents:UIControlEventTouchUpInside];
    [self.socialButton_ setCenterY:self.profileButton_.centerY];
    [self.socialButton_ setImage:[UIImage imageNamed:@"social_icon.png"] forState:UIControlStateNormal];

    self.notesButton_ = [[UIButton alloc] initWithFrame:CGRectMake(self.socialButton_.frameRight + 10, 0, 30, 30)];
    [self.notesButton_ addTarget:self action:@selector(showNotesView:) forControlEvents:UIControlEventTouchUpInside];
    [self.notesButton_ setCenterY:self.profileButton_.centerY];
    [self.notesButton_ setImage:[UIImage imageNamed:@"notes_icon.png"] forState:UIControlStateNormal];


    [self.containerView_ addSubview:self.profileButton_];
    [self.containerView_ addSubview:self.notesButton_];
    [self.containerView_ addSubview:self.socialButton_];

    [self.navigationBar addSubview:self.containerView_];

我可以看到 UIButton 但容器背景甚至不是黄色。当我按下按钮时,不会调用 UIButton 的操作。知道这是为什么吗?

【问题讨论】:

    标签: iphone ios objective-c ipad


    【解决方案1】:

    你是 UINavigationController 的子类?

    引用 Apple 的话:“这个类不适合子类化。”

    调整 UINavController 的常用方法是从包含的 UIViewController 的角度访问导航栏。您可以将目标/操作方法放在 UIViewController 中。

    【讨论】:

      猜你喜欢
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多