UIbutton not on the top of tableViewHeader isn't tapped - ios

I want to make a tableViewHeader from xib.
In xib I have the following hierarchy of views
But this way ActionButton isn't tapped, nothing overlays it and userInteractionEnabled = YES for it.
The button becomes active only when I put it into ZeroScreenView, so the only workaround I found is to place button directly on the ZeroScreenView.
Does someone know why does this it happen?
Is there any way to make the button tappable when it lays onto Labels Container?

Don't know what problem you are getting. But through code I have done like this
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,40)];
headerView.backgroundColor = [UIColor grayColor];
UIButton *buttn = [UIButton buttonWithType:UIButtonTypeCustom];
[buttn setTitle:#"" forState:UIControlStateNormal];
[buttn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
buttn.titleLabel.font = CUSTOM_FONT_ROMAN(15);
[buttn setFrame:headerView.frame];
[buttn setBackgroundColor: [UIColor clearColor]];
[buttn addTarget:self action:#selector(tapButton) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:buttn];
self.tableView.tableHeaderView = headerView;

Related

Get rid of padding for UITableViewCell custom accessoryView

I have a table view cell with a custom accessoryView, but it seems that the way it's laid out by default creates a 15-point margin on the right side. I want to get rid of it.
I can achieve the desired effect by overriding layoutSubviews, but that breaks edit mode animation (the accessory no longer slides in/out).
Any better way to do this?
Add subview instead of accessoryView
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame = CGRectMake(cell.contentView.frame.size.width-55, 2, 50, 50);
[deleteBtn setBackgroundImage:[UIImage imageNamed:#"trash.png"]
forState:UIControlStateNormal];
deleteBtn.backgroundColor = [UIColor clearColor];
//deleteBtn.alpha = 0.5;
deleteBtn.tag = indexPath.row;
[deleteBtn addTarget:self action:#selector(numberDeleteConfirm:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteBtn];

Adding a close button to the modal view that is partially off the view

I am trying to add a "X" button to the top of the modal view so if the user presses that it closes the modal view. I looked at the solution proposed here how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?
But I have two follow up questions here as i am fairly new to ios programming
The solution proposed in the above page does not seem to work for me. I tried it with a normal button first in the viewDidLoad of the modal VC and I am seeing it appear only within the bounds of modal view and not outside as I want.
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
buttonView.backgroundColor = [UIColor brownColor];
CGRect buttonFrame = CGRectMake( 0, 0, 100, 50 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: #"Close" forState: UIControlStateNormal];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[button addTarget:self
action:#selector(closeButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
CGRect parentView = self.view.superview.frame;
CGRect currentView = self.view.frame;
CGRect buttonViewFrame = buttonView.frame;
buttonViewFrame.origin = CGPointMake(parentView.origin.x - buttonViewFrame.size.width/2.0f, parentView.origin.y - buttonViewFrame.size.height/2.0f);
[buttonView setFrame:buttonViewFrame];
[self.view addSubview:buttonView];
what I am seeing is
How do I draw the "X" button do I use CGRect to draw it our or instead of adding a button in the above example should I just add a image with the "X" in it as the subview?
thanks in advance for the help
do like this, hope helps u :)
//adding button to your view
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 300, 350)];
aView.backgroundColor = [UIColor blueColor];
aView.tag = 100;
[self.view addSubview:aView];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitle:#"CLOSE" forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame = CGRectMake(aView.bounds.origin.x, aView.bounds.origin.y, 80, 30);// adjust with respect to bounds
aButton.backgroundColor = [UIColor redColor];
[aView addSubview:aButton];
[aView release]; //i am doing without ARC
The other option is to create a larger view and put your UITableView within this, along with your close button. The close button can then be at position (0,0) and the UITableView can start at (25,25).
This prevents complications - I have seen clipped views fail to recognise the touch outside of the main view, so your 50x50 button only has a touch area of 25x25.

Custom UIButton in UIscrollview

I am using xcode 4.6 to develop an app. Here i want to add UIButton programmatically to UIscrollview. This is the code i follow.
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Now the problem is that Button gets disappeared (technically its textcolor becomes white) on click. I checked keeping UIscrollview color to red that th button was still in the view but i cant get the reason why its text color changed and how do i undo dis.
Basically I wan to create a clickable link using UIbutton.
I know uitextview approach (datadetectortype) but its of no use as i want to show different text in the label for the link and the actual link.
Note: The textcolor doesnt change back to blue and remains white only.
Thanks in advance.
Try the below code
UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0);
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.scrollTest addSubview:bt];
-(void)userTappedOnLink:(UIButton*)sender
{
NSLog(#"Test ..");
[self performSelector:#selector(changeBtnTextColor:) withObject:sender afterDelay:1.0];
}
-(void)changeBtnTextColor:(UIButton*)btn
{
btn.titleLabel.textColor=[UIColor blueColor];
}
hope it will work for you.
use below code you will get your solution
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
[bt setBackgroundColor:[UIColor grayColor]];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Use the below code:
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Problem is you are set Title to titleLabel and you are changing textColor of buttonTitle color.
All the best !!!
check Button's Fram .. is it valid or not ??
and remove bt=[UIButton buttonWithType:UIButtonTypeCustom]; line from your code.

UIButton inside UIImageView does not respond to taps

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.

Using custom UITableViewCell's accessory view to show two items

I've been looking for this for a while, but haven't found an answer (blame poor googling skills). I have a custom UITableViewCell class which, currently, consists of a custom UISwitch and a UILabel. I want to add a button that's only visible (And active) when the switch is set to "Yes". Right now I add the switch to the accessoryView, and leave it. However, the accessory view doesn't really have subviews, as far as I can tell, so here's my question:
Should I just create a UIView that has a button and a switch, size it to fit the cell's accessory view (or will it auto-size itself?), and put that in as the cell's accessory view? And is this typically the way that it's gone about?
Or is there a solution that I'm missing?
Thanks.
Here is an example:
UIButton* btdel = [[UIButton alloc] init];
btdel.tag = indexPath.row;
//[btdel setTitle:#"Delete Event" forState:UIControlStateNormal];
[btdel setBackgroundImage:[UIImage imageNamed:#"ButtonRemove.png"] forState:UIControlStateNormal];
[btdel addTarget:self action:#selector(deleteEvent:) forControlEvents:UIControlEventTouchUpInside];
// bt.titleLabel.frame = CGRectMake(0, 0, 95,24);
btdel.frame = CGRectMake(110, 0, 30,30);
[headerView addSubview:btdel];
[btdel release];
UIButton* bt = [[UIButton alloc] init];
bt.tag = indexPath.row;
[bt setTitle:#"Select a Dress" forState:UIControlStateNormal];
[bt setBackgroundImage:[UIImage imageNamed:#"findDress.png"] forState:UIControlStateNormal];
[bt addTarget:self action:#selector(showDresses:) forControlEvents:UIControlEventTouchUpInside];
bt.font=[UIFont systemFontOfSize:(CGFloat ) 13];
// bt.titleLabel.frame = CGRectMake(0, 0, 95,24);
bt.frame = CGRectMake(0, 3, 95,24);
[headerView addSubview:bt];
cell.accessoryView = headerView;

Resources