簡體   English   中英

使用按鈕在UINavigationBar中添加子視圖不起作用

[英]adding subviews in UINavigationBar with buttons doesn't work

我有一個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的動作。 知道為什么會這樣嗎?

你是UINavigationController的子類?

引用Apple:“這個類不用於子類化。”

調整UINavController的常用方法是從包含的UIViewController的perspecitve訪問navBar。 您可以將目標/操作方法放在UIViewController中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM