I added a new UIButton
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newButton setFrame:CGRectMake(x, y, width, height)];
[newButton setTitle:#"" forState:UIControlStateNormal];
[newButton setTag:x+INDEX_OFFSET];
[newButton setBackgroundImage:image forState:UIControlStateNormal];
[newButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:newButton];
[self bringSubviewToFront:newButton];
and I can see it on the screen but it is not responding to a tap gesture. It should call "buttonPressed". Any help would be super!
One of the possible problem is that you are adding the button in a frame position out its superview. Just to check it easily try to set clipToBounds property to YES in its superview bounds, then run the app. If you don't see the button it means that you set the button position out superivew position, that's why it doesn't respond to touches.
Another possible problem is if you add the UIButton as a subview to an UIImageView (which you shouldn't, but it works). In this case, you could either add it to a UIView or set UIImageView's userInteractionEnabled property to YES.
Related
I have set an image in a UIButton but I need to set the title above this image. How can I do this?
[button setImage:[UIImage imageNamed:#"btn_select_s.png"] forState:0];
[button setImage:[UIImage imageNamed:#"btn_select_p.png"] forState:1];
[button setTitle:#"my title" forState:0];
You can use UIButton's titleEdgeInsets and imageEdgeInsets to position title and image as you want.
See Apple's UIButton reference documentation
For example, in a UIButton subclass:
[self setImageEdgeInsets:UIEdgeInsetsMake(imageFrame.origin.y, imageFrame.origin.x, 0, 0)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(labelFrame.origin.y, labelFrame.origin.x, 0, 0)];
You need to set the background image of the button instead:
[button setBackgroundImage:[UIImage imageNamed:#"foo"] forState:UIControlStateNormal];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
//Use you values for edge insets here.
[button setTitleEdgeInsets:UIEdgeInsetsMake(20.0f, 10.0f, 0.0f, 0.0f)];
There is a convenient UIButton subclass here:
https://github.com/lionhylra/FlexButton
It has features like:
- It provides 4 layout styles.
- It works as simple as UIButton.
- When you adjust the size of button, the size of imageView and titleLabel will change accordingly and correctly. They will not be put into disorder. This is the essential difference to using edge inset.
- TitleLabel and imageView will always be centered vertically and horizantally.
The UIButton has a cool attribute that is called "Shows Touch On Highlight" that allows me to tell the user "you touched this button".
Is there any way I can implement this for a UIImage? I want the user to see the point touched inside the UIImage.
If you are using a little trick. make a transparent button above UIImageView. so must set a UIImageView userInteractionEnabled true bec default false. as follows:
imageView.userInteractionEnabled = YES;
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame = imageView.frame;
clearButton.showsTouchWhenHighlighted = YES;
[clearButton setBackgroundColor:[UIColor clearColor]];
[imageView addSubView:clearButton];
other trick, create a transparent button, and set image your want.
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
clearButton.showsTouchWhenHighlighted = YES;
[clearButton setImage:[UIImage imageNamed:#"your_image"] forState:UIControlStateNormal];
[clearButton setBackgroundColor:[UIColor clearColor]];
No, it doesn't in same way as in UIButton, but you can use UIButton of type custom, instead to solve it for you.
Another help with UIImageView is setting it's Highlight image, but still you have to work for effect.
When I'm clicking on a UIButton that located on the top of UINavigationBar I have this glossy effect:
How can I add this effect to another button that located on a UINavigatonBar but not as a left or right button item? like this:
UIButton * emptyButton = [UIButton buttonWithType:UIButtonTypeCustom];
[emptyButton setFrame:CGRectMake(119, 5, 82, 35)];
[emptyButton addTarget:self action:#selector(centerNavigationBarTitleClicked:) forControlEvents:UIControlEventTouchUpInside];
[emptyButton setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar addSubview:emptyButton];
Thanks!
You simply have to set the showsTouchWhenHighlighted property to YES to get the glow effect, i.e.
emptyButton.showsTouchWhenHighlighted = YES;
I've created a tableview that has custom cells (I've subclassed UITableViewCell).
I'm creating several UIButtons in it. Declaring this in the .h file:
#property (nonatomic, strong) UIButton *myButton;
and this in the .m file:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(-100.0, 285.0, 60.0, 30.0)];
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
myButton.backgroundColor = [UIColor clearColor];
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[myButton setTitle:#"text" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:myButton];
I'm moving the cell to the right using UIPanGestureRecognizer and then the UIButton is revealed (notice that it is in a negative X index).I can see it but it is not clickable (I guess it is not in the view any more).
Any suggestion about how to make it clickable?
More details:
In the init of the cell, I'm doing:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
panGesture.delegate = self;
[self.contentView addGestureRecognizer:panGesture];
Then I implemented the gesture:
-(void)handlePanGesture:(UIPanGestureRecognizer *)sender {
CGPoint translate = [sender translationInView:self.contentView];
CGRect currentFrame = self.contentView.frame;
switch ([sender state]) {
case UIGestureRecognizerStateChanged:
currentFrame.origin.x = translate.x;
self.contentView.frame = currentFrame;
break;
default:
break;
}
}
When the frame moves to the right - the button is revealed. But not clickable...
If a button's frame is outside that of its parent's frame, it will not register touches. In this case, you mention that your button stops working when its frame's x location is set to -100. This makes sense because it is outside the touch area.
Your pan gesture you implemented moves the cell's view, so while the button may appear on screen, it is still OUTSIDE of the cell's view, hence it cannot receive touches.
One possible suggestion for fixing this would be to make your cells bigger from the start. So for example, if a cell's frame size is currently 0,0,320,44, you could instead make it -100,0,420,44 and place the contents of your cell accordingly. Now, your button's frame would change to 0,0,x,y and it would be inside of the cell's view and able to receive touches.
I am implementing UIButton and my UIControlEventTouchUpInside event does not fire, even though UIControlEventTouchDown does fire.
UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *filePathImage = #"img.png";
NSString *filePathImageTap = #"img-tap.png";
UIImage *buttonImage = [UIImage imageWithContentsOfFile:filePathImage];
UIImage *buttonImageTap = [UIImage imageWithContentsOfFile:filePathImageTap];
[btnClose setImage:buttonImage forState:UIControlStateNormal];
[btnClose setImage:buttonImageTap forState:UIControlStateSelected];
[btnClose setImage:buttonImageTap forState:UIControlStateHighlighted];
[btnClose addTarget:self action:#selector(close:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnClose];
I have an assumption why it doesn't take the UIControlEventTouchUpInside:
I had two "BannerView" (Advertisement) in two different ViewControllers. In one it just worked fine in the other I saw how the UIButton was touched and the second image (darker) appeared. But the selector was not hit. Then I realized, that the Up-Event was absorbt by a UISwipeGestureRecognizer from a UIImageView below. Some Recognizer compete against each other. I searched for an overview to see witch ones these are, but found nothing.
Another solution:
If you see that the ButtonState doesn't change u have to look if you have a View over your Button.