[英]Can't touch UIButton inside custom programmatic UIView
我有MasterChatViewController
,其中添加了名為JFShapedNavBar
的自定義UIView作為子視圖。 在JFShapedNavBar
內部,我添加了帶圖像的UIButton
子視圖,並向僅向控制台打印內容以便立即進行調試的方法添加了target-action。
MasterChatViewController
包含JFShapedNavBar
而 JFShapedNavBar
包含UIButton
。
我使用Masonry
來設置約束。 當我初始化視圖時,我使用initWithFrame:CGRectZero
的自定義變體:
_shapedNavBar = [[JFShapedNavBar alloc] initWithFrame:CGRectZero
forViewController:self];
[self.view addSubview:_shapedNavBar];
[_shapedNavBar mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.equalTo(self.view);
}];
在JFShapedNavBar.m
內部:
-(void)configureRightButton {
// Right Button of Nav
UIImage *buttonImage = [UIImage imageNamed:@"LobbyButton"];
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_rightButton setImage:buttonImage forState:UIControlStateNormal];
[_rightButton addTarget:self action:@selector(testTouch) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_rightButton];
[self setNeedsUpdateConstraints];
}
-(void)testTouch {
NSLog(@"You managed to figure out what you were doing wrong.");
}
-(void)updateConstraints {
[super updateConstraints];
[_arcShadow mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.equalTo(self);
make.height.equalTo(__navHeight);
}];
[_arcDrop mas_remakeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_arcShadow);
make.height.equalTo(_arcShadow.mas_height);
}];
[_rightButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(24);
make.trailing.equalTo(self).offset(-16);
make.height.equalTo(@30);
}];
}
最終,我試圖創建一個自定義的導航欄(但不是UIKit中的子類)。 我進行了大約2個小時的實驗,試圖弄清楚為什么目標動作處理程序不觸發,但是現在我很沮喪,並且沒有主意。 為什么我不能觸摸 UIButton?
也許這很重要,我不知道,但是在我的視圖控制器MasterChatViewController
我已隱藏了導航欄self.navigationController.navigationBarHidden = YES;
這不會解決您的問題,而只是調試可輕敲問題的一些標准操作
1)暫時將所有按鈕的背景設置為綠色(這樣您就可以看到其點擊區域擴展到了
2)在子級和超級容器上設置userInteraction = YES(有時)
3)注意任何可能會吞噬您的觸摸的手勢識別器
4)確保容器視圖內的按鈕在其邊界之內..如果不是,則將其不可點擊
5)要進一步調查布局問題,請使用Reveal或內置rip的xcode進行視圖調試http://www.raywenderlich.com/98356/view-debugging-in-xcode-6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.