I have MasterChatViewController in which I added as a subview a custom UIView called JFShapedNavBar. Inside the JFShapedNavBar I added a subview of UIButton with an image and added a target-action to a method that just prints something to the console in order to debug right now.
MasterChatViewController contains JFShapedNavBar and
JFShapedNavBar contains UIButton.
I use Masonry to setup constraints. When I initialize the views I use a custom variant of 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);
}];
Inside the 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);
}];
}
Ultimately, I am attempting to create a custom navigation bar (but not a subclass of the one in UIKit). I experimented for approximately 2 hours trying to figure out why the target-action handler doesn't fire, but now I am frustrated and out of ideas. Why can I not touch the UIButton?
Maybe this matters, I don't know, but on my view controller MasterChatViewController I have set hidden the navigation bar self.navigationController.navigationBarHidden = YES;
CGRectZero, but for some reason I expected it to resize itself based on its content. It was displaying correctly, after all. I found the problem by observing the frame of theshapedNavBarand noticed it was(0, 0, 375, 0). - nodebase