button Highlighted not work when interactivePopGestureRecognizer.enabled = YES - ios

Use interactivePopGestureRecognizer when popViewController.
Set Custom back button and keep interactivePopGestureRecognizer = YES.
- (void)setNavigation {
[self.navigationController.scrollNavigationBar setNavigationTitleColor:[UIColor whiteColor]];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back"]
style:UIBarButtonItemStyleDone
target:self
action:#selector(popViewController)];
backButton.tintColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = backButton;
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
}
have a likeButton on the ViewController.
- (YMFeedLikeButton *)likeButton {
if (!_likeButton) {
YMFeedLikeButton *likeButton = [[YMFeedLikeButton alloc] init];
replyButton.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, 150, 50);
[likeButton setImage:[UIImage imageNamed:#"topic-icon-like.png"]
forState:UIControlStateNormal];
[likeButton setImage:[UIImage imageNamed:#"topic-icon-like.png"]
forState:UIControlStateNormal | UIControlStateHighlighted];
[likeButton setImage:[UIImage imageNamed:#"topic-icon-liked.png"]
forState:UIControlStateSelected];
[likeButton setImage:[UIImage imageNamed:#"topic-icon-liked.png"]
forState:UIControlStateSelected|UIControlStateHighlighted];
UIImage *highlight = [UIImage imageNamed:#"highlight.png"];
[likeButton setBackgroundImage:highlight
forState:UIControlStateHighlighted | UIControlStateSelected];
[likeButton setBackgroundImage:highlight
forState:UIControlStateHighlighted | UIControlStateNormal];
[likeButton setBackgroundImage:highlight
forState:UIControlStateHighlighted];
[likeButton addTarget:self action:#selector(like)
forControlEvents:UIControlEventTouchUpInside];
[likeButton setTitleColor:[UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:1.0f]
forState:UIControlStateNormal];
[likeButton setTitleColor:[UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:1.0f]
forState:UIControlStateSelected];
[likeButton setImageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
likeButton.value = 0;
[self insertSubview:_likeButton = likeButton atIndex:0];
}
return _likeButton;
}
likeButton Highlighted is not working when i clicked it.
If close interactivePopGestureRecognizer
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
or change likeButton.frame = CGRectMake(150, 150, 150, 50);
,likeButton Highlighted touch is working.
i hope likeButton Highlighted is working when usedinteractivePopGestureRecognizer.

It's late, but anyway... This solution works for me:
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationController.interactivePopGestureRecognizer.cancelsTouchesInView = NO;
self.navigationController.interactivePopGestureRecognizer.delaysTouchesBegan = NO;
self.navigationController.interactivePopGestureRecognizer.delaysTouchesEnded = NO;

Related

UINavigationController centered button using obj-c

I'm trying to add a centered button in my navbar header, confused as to how I can do it
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIBarButtonItem *meButton = [[UIBarButtonItem alloc] initWithImage:meImage style:UIBarButtonItemStylePlain target:self action:#selector(twitterButton)];
[self.titleView //i read this can be used to like set it but i havent been able to figure it out
help would be appreciated, pretty new
check this code
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
container.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 44, 44)];
[btn setBackgroundImage:[UIImage imageNamed:#"button0.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
//add your spacer images and button1 and button2...
self.navigationItem.titleView = container;
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender {
NSLog(#"button clicked");
}
I think its pretty straight, you can use titleView property of UINavigationItem :
-(void)addCenterButton {
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIButton *button = [[UIButton alloc] initWithFrame::CGRectMake(0, 0, 40, 20)];
[button setImage:meImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(twitterButton) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;
}

UIButton image not showing up on normal state

I am trying to have a checkbox using button and this is what I am doing:
int checkBoxWidth = foregroundHeight / 12.0;
int checkBoxHeight = foregroundHeight / 12.0;
UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, checkBoxWidth, checkBoxHeight)];
//[checkBox setBackgroundImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
//[checkBox setBackgroundImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
[checkBox setImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
checkBox.layer.borderWidth = 1.0;
checkBox.layer.borderColor = [Utility primaryColor].CGColor;
checkBox.layer.cornerRadius = cornerRadius;
[switchView addSubview:checkBox];
but the image appears only when I touch the button, in any other state image is not showing up. Is there something missing?
You have to change the button state for the image to be changed. Use the below code to achieve your desired result.
int checkBoxWidth = foregroundHeight / 12.0;
int checkBoxHeight = foregroundHeight / 12.0;
UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, checkBoxWidth, checkBoxHeight)];
[checkBox setImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateHighlighted];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected | UIControlStateHighlighted];
[checkBox addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
checkBox.layer.borderWidth = 1.0;
checkBox.layer.borderColor = [Utility primaryColor].CGColor;
checkBox.layer.cornerRadius = cornerRadius;
[switchView addSubview:checkBox];
And add this target function
-(IBAction)buttonClicked:(UIButton*)sender{
sender.selected = !sender.selected;
}

RightBar Button on navigation bar is not giving click like look

I want to show image and the title on RightBar Button so i have written code as below:
UIBarButtonItem *negativeSpacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
negativeSpacer.width = BARBUTTONITEM_PADDING_IPAD;
UIImage *buttonImage = [UIImage imageNamed:#"select-ipad#3x.png"];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setImage:buttonImage forState:UIControlStateNormal];
[selectButton setTitle:#"Select" forState:UIControlStateNormal];
[selectButton setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[selectButton.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[selectButton sizeToFit];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:selectButton];
[selectButton addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,aBarButtonItem,nil]];
This code shows the title and image on the NavigationBar and when i click on it, then the clicked like appears only on the image but not on the text.
-(void)viewWillAppear:(BOOL)animated{
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onBtnRightClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnRight=[[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = btnRight;
}
-(IBAction)onBtnRightClick:(id)sender
{
NSLog(#"Right Bar Button Clicked.");
}
Using your code, try 2 buttons:
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(0.0, 0.0, 45.0, 45.0)];
[button1 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc]initWithCustomView:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"Select" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button2.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[button2 sizeToFit];
UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
self.navigationItem.rightBarButtonItems = #[buttonItem2,buttonItem1];
I hope this can help you.

Adding custom back button on navigation bar

I am trying to add custom back button on navigation bar but it is not working below is my code
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.backBarButtonItem = aBarButtonItem;
}
Please tell what's wrong with this code
Try this.
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}
Here is the code that i have used to create custom backbutton
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the custom back button
//add the resizable image
UIImage *buttonImage = [[UIImage imageNamed:#"backbtn.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
lbl.text = #"Settings";
lbl.backgroundColor = [UIColor clearColor];
//lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0];
lbl.numberOfLines = 0;
lbl.lineHeight = 12;
lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
lbl.font = [UIFont fontWithName:#"Helvetica" size:12];
[button addSubview:lbl];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
-(void)back {
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
This code will resize your image so it can fit the text.
You can also put this button in any method.
You didn't add action to your button. So you need to add action to your button. For more info see in the below code..
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside]; // -------- Edit here -----
self.navigationItem.backBarButtonItem = aBarButtonItem;
}
-(void)backBtnPressed
{
[self.navigationController popViewControllerAnimated:YES];
}

Change label when a button has been pressed

I have a button which changes images which it has been pressed, it goes from img1 to img2. I now also want the text to be changed when that certain button been pressed. So it starts on "hello", and then when pressed it turns to "goodbye" for example.
this is my code at the moment:
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"greypad.png"]) ? #"greenpad.png" : #"greypad.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
ViewDidLoad:
[self addLabel];
self.btn1ImageName = #"greypad.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(46, 67, 48, 48)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(toggleImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
The label?
-(void)addLabel{
UILabel *aLabel = [[UILabel alloc]initWithFrame: CGRectMake(20, 200, 280, 80)];
aLabel.numberOfLines = 0;
aLabel.textColor = [UIColor blueColor];
aLabel.backgroundColor = [UIColor clearColor];
aLabel.text = #"hello";
[self.view addSubview:aLabel];
}
Try this
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"greypad.png"]) ? #"greenpad.png" : #"greypad.png";
self.titleStr = ([self.titleStr isEqualToString:#"hello"]) ? #"goodbye" : #"hello";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 setTitle:self.titleStr forState:UIControlStateNormal];
}
I would recommend a different approach:
When you set up your button, set different title and image for different states
[btn1 setTitle:#"hello" forState: UIControlStateNormal];
[btn1 setTitle:#"goodbye" forState: UIControlStateSelected];
[btn1 setImage:[UIImage imageNamed:#"greypad.png"] forState:UIControlStateNormal];
[btn1 setImage:[UIImage imageNamed:#"greenpad.png"] forState:UIControlStateSelected];
And in your toggle method simply do
- (void)toggleImage:(UIButton *)button
{
button.selected = !button.selected;
}
Add this to your toggleImage Method:
[bt1 setTitle:#"goodbye" forState: UIControlStateNormal];

Resources