How to set subview as a first responder? - ios

I am trying to close the image View while clicking on close button.but button does not responds to touch event.
here is my code:
-(void)imageTouchUP:(id)sender
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.image=self.testimonialImage;
imageView.tag=1;
UIButton *closeButton=[UIButton buttonWithType:UIButtonTypeCustom];
closeButton.frame=CGRectMake(0, 0, 20, 20);
[closeButton setImage:[UIImage imageNamed:#"close.jpg"] forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:closeButton];
[self.view addSubview:imageView];
}
-(void)buttonClicked:(id)sender
{
UIView *subView=[self.view viewWithTag:1];
[subView removeFromSuperview];
}

For the UIImageView to handle touches you need to set userInteractionEnabled to YES, it defaults to NO.
Try:
[imageView setUserInteractionEnabled:YES];
Reference

There is 1 property of UIImageView userInteractionEnabled and its value is by default NO. So in your code add
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.userInteractionEnabled = YES;

Maybe there's another view with tag=1 in your self.view. Please try to edit button clicked like this:
- (void) buttonClicked:(UIButton *)btn
{
UIView *view = btn.superview;
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}

Related

Can't touch UIButton in UIScrollView

I am programmatically creating buttons in a view inside UIScrollView. I also have programmatically created UITextField. I can select and enter data into the text fields all day long. But I cannot get any sign the buttons are being touched. I can add them to the main view and they work. But when added to the view inside the UIScrollView, they are lost in another dimension. I've read plenty of info about setting userInteraction, delayContentTouches, and intercepting via gestureRecognizer. The last might be related to my problem. I return NO via [touch.view isDescendantOfView:self.myScrollViewName]. I can't get it to trigger via [touch.view isKindOfClass:[UIButton class]]. Which makes me think I have a more fundamental error I am missing?
related:
https://stackoverflow.com/a/6825839/4050489
edited to add button code per request:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = myButtonFrame;
self.myButton.enabled = interface.isActive;
self.myButton.userInteractionEnabled = YES;
self.myButton.exclusiveTouch = YES;
self.myButton.backgroundColor = [UIColor greenColor];
//self.myButton. = YES;
self.myButton.layer.borderWidth=1.0f;
self.myButton.layer.borderColor=[[UIColor blackColor] CGColor];
self.myButton.layer.cornerRadius = 10;
[self.myButton setTag:interface.tag];
[self.myButton setTitle:interface.Name forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.myButton setEnabled:YES]; //error check
[self.partSetupView addSubview:self.myButton];
partSetupView is a UIView in the UIScrollView.
Try this sample Code:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
myScroll.backgroundColor = [UIColor redColor];
[self.view addSubview:myScroll];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
myView.backgroundColor = [UIColor yellowColor];
[myScroll addSubview:myView];
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
myButton.backgroundColor = [UIColor blackColor];
[myView addSubview:myButton];
[myButton addTarget:self action:#selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
NSLog(#"Button Click");
}
#end

How to identify the subviews (imageviews) addeded to the UIButton in UIAutomation?

I have a UIButton and the button background is set to an image. Added
an image view on the top right corner of the button and I set it
to different image when the button is clicked to show the selection.
When I try to automate this using instruments. I don’t see any subviews
(image views) in the UIAutomator.logElementTree()method does not show
any image views.
How to identify the image views added as subview in the UIButton?
Here is the code.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(40, 40, 100, 100);
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#“testImage.png”] forState:UIControlStateNormal];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
imageview.frame = CGRectMake(70,30,20,20);
[button addSubview:imageview];
You have to set the accessibilityEnabled & accessibilityLabel.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(40, 40, 100, 100);
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
button.accessibilityEnabled = YES;
button.accessibilityLabel =#"My Button";
[button setBackgroundImage:[UIImage imageNamed:#“testImage.png”] forState:UIControlStateNormal];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
imageview.accessibilityEnabled = YES;
imageview.accessibilityLabel = #"My Image";
imageview.frame = CGRectMake(70,30,20,20);
[button addSubview:imageview];
Refer this tutorial
Sweet Angel's answer is enough if you have a single UIImageView subview. However, I would suggest using tags to identify subviews.
imageView.tag = 1346;//Any arbitrary number (I am unsure if there are any limitations to the values you can use, but all 4 digit numbers work fine for me).
Then to get the imageView:
UIImageView* imageView = (UIImageView*)[button viewWithTag:1346];
That's it.
If you want to identify imageview on button taped, try following code :
- (IBAction)didTapButton:(UIButton *)sender
{
for (id view in sender.subviews)
{
if ([view isKindOfClass:[UIImageView class]])
{
NSLog(#"Image view captured.....");
}
}
}

show and remove same button in iOS

I have one UIButton. Now I add button on Subview :-
UIButton* button4 = [[UIButton alloc] initWithFrame:frame4];
[button4 setBackgroundImage:image3 forState:UIControlStateNormal];
[button4 setShowsTouchWhenHighlighted:YES];
[button4 addTarget:self action:#selector(Nofication:) forControlEvents:UIControlEventTouchDown];
- (void)Nofication:(id)sender
{
share=[[UIView alloc]init];
share.frame=CGRectMake(300, 60, 130, 100);
[self.view addSubview:share];
[UIView animateWithDuration:2.0
animations:^{
share.frame =CGRectMake(180, 50, 130, 100); // its final location
}];
share.backgroundColor=[UIColor yellowColor];
login=[[UIButton alloc]init];
login.frame=CGRectMake(0, 0, 130, 50);
[login setBackgroundImage:[UIImage imageNamed:#"login.png"] forState:UIControlStateNormal];
[login addTarget:self action:#selector(asubmit:) forControlEvents:UIControlEventTouchDown];
[share addSubview:login];
login.layer.borderColor = [[UIColor whiteColor]CGColor];
policies=[[UIButton alloc]init];
policies.frame=CGRectMake(0, 50, 130, 50);
[policies setBackgroundImage:[UIImage imageNamed:#"Policies.png"] forState:UIControlStateNormal];
[policies addTarget:self action:#selector(apolicies:) forControlEvents:UIControlEventTouchDown];
[share addSubview:policies];
}
When I click button it's open subview working nice. But now I need if subview is showing in that time click button it's need to hidden subview. If subview is not showing in that time when I click button it's need to show subview. So Please give me any idea about it.
How to Show and Hidden same UIButton
thanks in Advance .
There is a hidden property on UIView. https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instp/UIView/hidden
if (button.isHidden) {
button.hidden = NO;
}
Is it what asked?
Where subview is the view (or UIButton) you are trying to hide if shown, and show if hidden.
[subview setHidden:![subview isHidden]];
Edit :
In your Notification function :
// Be sure that the view is loaded, maybe load it at a different place or use a boolean
if([subview isHidden]){
[subview setHidden:false];
}else{
[subview setHidden:true];
}
You can set button4.selected = !button4.selected in Nofication: method.
Then you can check button4.isSelected and hide/show subview with hidden property
Hope it helps.

Delay of UIButton's highlighted state on iOS 7

I created new project in Xcode - Single View app.
App have only two buttons.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setBackgroundColor:[UIColor greenColor]];
[button1 setFrame:CGRectMake(0, self.view.frame.size.height-40-100, self.view.frame.size.width, 40)];
[button1 setTitle:NSLocalizedString(#"button 1", nil) forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setBackgroundColor:[UIColor greenColor]];
[button2 setFrame:CGRectMake(0, self.view.frame.size.height-40, self.view.frame.size.width, 40)];
[button2 setTitle:NSLocalizedString(#"button 2", nil) forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.view addSubview:button2];
When I run this app on iPhone with iOS 7 second button have delay of highlighted state when I press this button. On iPhone with iOS 6 second button works perfect.
Why button on iOS 7 have delay of highlighted?
My problem was I had a UIButton as a subview of a paging UIScrollView, so I wanted the user to be able to do a right swipe over the area where the button was without it pressing the button. In iOS6 if you do this over a rounded rect button, it works fine, but in iOS7 it also works but the button won't trigger it's highlight. So to fix this, I implemented my own UIButton using the longPressGestureRecognizer:
- (void) longPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer
{
if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan || longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];
if (CGRectContainsPoint(self.bounds, touchedPoint))
{
[self addHighlights];
}
else
{
[self removeHighlights];
}
}
else if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
if (self.highlightView.superview)
{
[self removeHighlights];
}
CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];
if (CGRectContainsPoint(self.bounds, touchedPoint))
{
if ([self.delegate respondsToSelector:#selector(buttonViewDidTouchUpInside:)])
{
[self.delegate buttonViewDidTouchUpInside:self];
}
}
}
}
Then when you initialize the longPressGestureRecognizer and you do:
self.longPressGestureRecognizer.minimumPressDuration = .05;
This will allow you to do a swipe over the button without it triggering it, and will also enable you to press the button and have its highlight trigger. Hope this helps.
Try to overload this method in your scroll view subclass:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
// fix conflicts with scrolling and button highlighting delay:
if ([view isKindOfClass:[UIButton class]])
return YES;
else
return [super touchesShouldCancelInContentView:view];
}
I'm not sure if the OP just wants visual feedback, but if so, setting the showsTouchWhenHighlighted property to YES/true in code or checking the Shows Touch On Highlight option in IB will accomplish that.

adding multiple subviews in ios

I'm trying to add multiple subview programmatically, but my buttons inside those subviews are not working. The buttons were also added as subviews programmatically.
Here's the code, hopefully will explain more about what I mean.
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(#"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:#"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
You need to increase the frame of the containerOfSelectedImage. Currently the width is 0.
The containerOfSelectedImage's frame has to be big enough so the button fits inside it. If the button is outside that frame it will show up but you can't touch him.
To debug this issues you can use a tool like Reveal App. If any of your buttons is outside the frame of the parent then the touch will not be detected.
Change [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; to [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

Resources