UIButton inside UIImageView does not respond to taps - ios

I have a scroll view, which has an image view with an image as a subview, and the image view has a UIButton as one of its subviews. The problem is, I am not able to click on the button. I can SEE the button, but I cannot tap on it.
Can someone tell me what is it that I am messing up? Any help is greatly appreciated! Thanks!
Below is the code:
scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"img.jpg"]];
scrollView.delegate = self;
self.view = scrollView;
// add invisible buttons
[self addInvisibleButtons];
[scrollView addSubview:imageView];
addInvisibleButtons has the following code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[self.imageView addSubview:button];

UIImageView has userInteractionEnabled set to NO/ false by default.
You are adding the button as a subview to the image view. You should set it to YES / true.

May I ask why are you adding invisible UIButtons to UIImageView?
Seems like a bad practice, notice Interface Builder doesn't allow you to add UIButton inside them.
If you want an image with touch handling, you can:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"img.jpg"] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
[scrollView addSubview:button];

You can have addInvisibleButtons implementation as
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor clearColor]];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
self.imageView.userInteractionEnabled = YES;
[self.imageView addSubview:button];
If you want to have UIButton as completely invisible then you need to remove a line
[button setTitle:#"point" forState:UIControlStateNormal];
since it use to show text on UIButton which make it visible.
This may resolve your issue.

Related

Changing EdgeInsets of Image and Title of UIButton makes the button looks blur

I am creating a UIButton programatically, when i set the EdgeInsets of button title and image, the button title and image got BLUR. (See reference screenshot)
Why title and image gets blur?
Here is my code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
CGRect buttonFrame = CGRectMake(0.0, 0.0, mainFrame.size.width, mainFrame.size.height);
button.frame = buttonFrame;
[button setTitle:#"Press me" forState:UIControlStateNormal];
[button setTitle:#"Pressed" forState:UIControlStateSelected];
[button.titleLabel setFont:[UIFont fontWithName:FNT_ALL_TEXTS size:10]];
[button setImage:[UIImage imageNamed:imageNormal] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:imageSelected] forState:UIControlStateSelected];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage * refimage = [UIImage imageNamed:imageNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(buttonFrame.size.height*0.5, -refimage.size.width*1.0, 0.0f, 0.0f)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, buttonFrame.size.height*0.35, 0.0)];
[self addSubview:button];
dear just make sure that it is content vertical and horizontal alignment for button however u use image or text in button.
UIButton *btn;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
// horizontal Alignment
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill
UIControlContentHorizontalAlignmentCenter
// Vertical Alignment
UIControlContentVerticalAlignmentTop
UIControlContentVerticalAlignmentFill
UIControlContentVerticalAlignmentBottom
UIControlContentVerticalAlignmentCenter

Adding an Image to my UIButton subview

I have a UIButton that is added on my super view as a subview, It is just a big block, how do I add an image to my button.
- (void)viewDidLoad
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:#selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}
you can add image in UIButton by following two ways.
1) With help of setting Background Image.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:#selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
2) Or you can add UIImageView as subview.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setClipsToBounds:YES];
[button setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
UIImageView * imgView = [[UIImageView alloc]init];
[imgView setImage:[UIImage imageNamed:#""]];
[imgView setFrame:CGRectMake(0, 0, 100, 100)];
[button addSubview:imgView];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:#selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
Use this:
-setBackgroundImage:forState:
you may looking for something like this, if you want to have an "image as button" and not only a default button with an image inside
BOOL ipad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];;
CGRect frame;
//universal app
if (ipad)
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
else
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
btn.titleLabel.backgroundColor = [UIColor clearColor];
forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"buttonAsImage.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
[btn setTitle:title forState:UIControlStateNormal];
There are different ways to do it.
First one is you can add an imageview to your superview and set imageview's image whatever you want. Then, at the same position add your button with size of imageview and set button color to clearColor. But you need to add imageView first otherwise your button will be under the image.
Second one is default image to button, use setImage:image forState:UIControlStateNormal or setBackgroundImage:image forState:UIControlStateNormal.
Got it, here is how:
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
Use this method.
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

UIScrollView error in iOS

I am new to UIScrollView. I don't know how to add buttons with them. In my app, I need a button in horizontal view. But when I tried to create the UIScrollView programatically, its not working. It is throwing some errors. pls help me
I tried to implement this using storyboard too, but failed as I dont how to connect them both using the viewDidLoad.
this is the code I have used so far:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.layerPosition = self.toplayer.frame.origin.x;
UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollview.showsHorizontalScrollIndicator=YES;
scrollview.userInteractionEnabled=YES;
scrollview.scrollEnabled=YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Excavations",#"Concrete",#"Framing",#"Insulation" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);
}
if I am doing wrong pls tell me the correct procedure to do it.
thanks
I can see just one error in your code now:
[button setTitle:#"Excavations",#"Concrete",#"Framing",#"Insulation" forState:UIControlStateNormal];
setTitle: expect just one string, try this:
[button setTitle:#"Excavations" forState:UIControlStateNormal];
You probably want to add this button thou your scroll view so you need to call:
[scrollview addSubview:button];
It should help, if not post your error.
I assume you want to pass UIControlEventTouchUpInside instead UIControlEventTouchDown as a button control event.
try this .it will work
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.layerPosition = self.toplayer.frame.origin.x;
UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollview.showsHorizontalScrollIndicator=YES;
scrollview.userInteractionEnabled=YES;
scrollview.scrollEnabled=YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Excavations" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);
}

xcode invisible button with text

I know I could do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
but that leaves a border around the button.
I could also do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then put a UILabel behind the button.
Is there a way to accomplish this ^ without needing two objects?
UPDATE:
To clarify my question:
I want the have text, only text, that when pressed performs a method call. For reasons that are complicated to explain I need to do this by way of a button. So, how do I make the text of a button the only visible part of the button. No border. No background. Just text.
UIButton *btnLikeUserCount = [[[UIButton alloc] init] autorelease];
[btnLikeUserCount.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
btnLikeUserCount.frame = CGRectMake(posx,posy,width,height);
[btnLikeUserCount setTitle:text forState:UIControlStateNormal];
[btnLikeUserCount setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
[btnLikeUserCount addTarget:self action:#selector(btnLikeUserCountClick:) forControlEvents:UIControlEventTouchUpInside];
no need to take label object.
I can't get what you want exactly but you might be missed [self.view addSubview:button] in your code. so you can not display button with text.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Your second code is working fine. In the below image hello is a button.
My code is:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(110, 100, 100, 50);
[button setTitle:#"Hello" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
in this way u can do this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 400, 100, 50);
[button setTitle:#"Button text" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button.layer setBorderColor: [UIColor clearColor]];
[button.layer setBorderWidth: 0.0];
[self.view addSubview:button];

Update UIButton title after receiving reply from SKProductsRequest for showing in-app purchases price

I'm trying to create two buttons whose label is initially set as a default value. The two buttons are managed by a popOverViewController, in which the viewDidLoad method initialise all the controls. I also have another method called addButtons that gets executed by the AppDelegate after receiving a valid response from Apple's servers containing products descriptions and prices.
My problem is that I can't get the label updated with that method.
My viewDidLoad method in the popOverViewController is:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Add a label to the popover's view controller.
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(button1Week:)
forControlEvents:UIControlEventTouchDown];
NSString *priceWeekly = [NSString stringWithFormat:#"weekly subscription"];
[button setTitle:priceWeekly forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 200.0, 50.0);
[button2 addTarget:self
action:#selector(button1Month:)
forControlEvents:UIControlEventTouchDown];
[button2 setTitle:#"monthly subscription" forState:UIControlStateNormal];
button2.frame = CGRectMake(0.0, 55.0, 200.0, 50.0);
[self.view addSubview:button];
[self.view addSubview:button2];
}
and my update label method, of which I'm sure it gets called at the right time, is:
-(void)addButtons {
[self.button setTitle:#"updated label" forState:UIControlStateNormal];
[self.button2 setTitle:#"updated label 2" forState:UIControlStateNormal];
}
can you please check my code and suggest the right way to update the labels? Basically I want to initialize the controls with a default label because of the waiting time between controls creation and server response.
UPDATE:
I have updated the code as follows, but still everything that's inside the addButtons method gets executed as I have an NSLog to check, but the button title still remains as created in viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Add a label to the popover's view controller.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ilManifestoAppDelegate *delegate = (ilManifestoAppDelegate*)[[UIApplication sharedApplication] delegate];
[button addTarget:self
action:#selector(button1Week:)
forControlEvents:UIControlEventTouchDown];
// [button setTitle:priceWeekly forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 200.0, 50.0);
[button2 addTarget:self
action:#selector(button1Month:)
forControlEvents:UIControlEventTouchDown];
// [button2 setTitle:#"monthly subscription" forState:UIControlStateNormal];
button2.frame = CGRectMake(0.0, 55.0, 200.0, 50.0);
// [self addButtons];
NSString *weekPrice = [NSString stringWithFormat:#"settimanale a %#",delegate.priceWeek];
[button setTitle:weekPrice forState:UIControlStateNormal];
[button2 setTitle:#"updated label 2" forState:UIControlStateNormal];
NSLog(#"week price %#", weekPrice);
[self.view addSubview:button];
[self.view addSubview:button2];
}
-(void)addButtons {
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setTitle:#"modificato" forState:UIControlStateNormal];
[self.view addSubview:button];
NSLog(#"addButtons executed");
}
In viewDidLoad you are using button and button2. In addButtons you are using self.button and self.button2. Did you ensure that these are the same buttons?
Also place a breakpoint in addButtons to see if it really is called, if you haven't done this yet.
Have you also created the outlet to the buttons in the xib, if you haven't placed them only with code.

Resources