iOS system button click frame issue - ios

Problem:
Some system UIButton Types are getting clickable outside its defined frame/bounds.
The below two buttons types are getting selected within their defined frame region properly.
UIButtonTypeCustom [width:160, height:44.0]
UIButtonTypeSystem [width:50, height:44.0]
The below button types are even selectable outside of its frame rectangle from all sides by 10~15points.[Background color is drawn/displaying with in the defined frame for all these buttons]
UIButtonTypeDetailDisclosure,[width:50, height:44.0]
UIButtonTypeInfoLight, [width:50, height:44.0]
UIButtonTypeInfoDark, [width:50, height:44.0]
UIButtonTypeContactAdd [width:50, height:44.0]
I expecting the clickable/selectable area should be with in the button frame.
I am creating the buttons using following method:
- (UIButton*)createButton:(UIButtonType)style withFrame:(CGRect)frame {
UIButton *button = nil;
UILabel *label = nil;
switch(style)
{
case UIButtonTypeCustom: // frame: {x,y,160.0,44.0}
button = [[UIButton alloc] initWithFrame:frame];
button.layer.cornerRadius = 10;
label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 44.0)];
label.text = #"Custom";
label.textAlignment = NSTextAlignmentCenter;
[button addSubview:label];
break;
case UIButtonTypeSystem: // frame: {x,y,50.0,44.0}
button = [UIButton buttonWithType:style];
[button setTitle:#"Button" forState:UIControlStateNormal];
button.frame = frame;
break;
default: // frame: {x,y,50.0,44.0}
button = [UIButton buttonWithType:style];
button.frame = frame;
break;
}
// Background whitecolor is displaying properly for all buttons.
button.backgroundColor = [UIColor whiteColor];
return button;
}
The createButton method is called as below
for (int i=0, y=10, x=15; i<6; ++i) {
if(i == 5)
{
y += 90;
x = ([[UIScreen mainScreen] bounds].size.width - 160.0)/2.0;
}
UIButton *btn = [self createButton:5-i withFrame:CGRectMake(x, y, ( i<5 ? 50.0 : 160.0), 44.0)];
[btn setTag: i+1];
[btn addTarget:self action:#selector(settingsView:) forControlEvents:UIControlEventTouchUpInside];
[_subView addSubview:btn];
x += 60;
}

The iOS typically expands the touchable area of buttons according to its own internal rules.
My guess: it makes the buttons easier for users to touch.

Related

getting element from array one by one and createbutton

I have a UItextFeild as search bar on clicking a tableview with suggestion open.now i want to click on that row and make a button with its label in below view as described in image .how can i acheive this
I want it one by one means when i click on row it create a button on view and UITableView hide then again i start searching and on again clicking on some other row make a button with different value of X.
_selectednames = [NSMutableArray arrayWithCapacity:self.sortedArray.count];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// MGContactItemsModel *model = _arrayForContacts[indexPath.row];
self.searchText.text = [NSString stringWithFormat:#"%#",[_sortedArray objectAtIndex:indexPath.row]];
self.tableView.hidden = YES;
[_selectednames addObject:_searchText.text];
for (int i; i < _selectednames.count ; i++) {
[self makeLabelsAndButtons:i];
}
now in my makeLabelsAndButtons funtion i done something like this
CGFloat xOffset = 10.0f;
int buttonCount = 0;
CGRect buttonFrame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
NSString *nameString = [self.selectednames objectAtIndex:indexPath];
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//_button.frame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
for(int i=buttonCount; i >0;i++) {
xOffset = xOffset+10;
buttonFrame.origin.x += buttonFrame.size.width+xOffset;
}
_button.frame = buttonFrame;
buttonCount++;
_button.tag = indexPath;
//button.center = CGPointMake(xOffset, 10.0f);
//[button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[_button setTitle:self.selectednames[indexPath] forState:UIControlStateNormal];
_button.backgroundColor = [UIColor lightGrayColor];
_button.titleLabel.font = [UIFont fontWithName:#"Arial" size:8.0f];
_button.layer.cornerRadius = 10.0f;
_button.layer.masksToBounds = YES;
[self.buttonView addSubview:_button];
NSLog(#"name: %#", nameString);
also on clicking button it will removed.any help would be thankful.
It works for me.Please try it.
y=20;
x=20;
for (int i=0; i<[arrAssignUsers count]; i++) {
CGRect screenRect=[[UIScreen mainScreen]bounds];
CGFloat screenWidth=screenRect.size.width;
[arrBtnStatus addObject:[NSNumber numberWithBool:NO]];
NSString *strNames=[arrAssignUsers objectAtIndex:i];
stringsize=[strNames sizeWithAttributes:
#{NSFontAttributeName: [UIFont systemFontOfSize:20.0f]}];
btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGFloat m=x+stringsize.width+10;
CGFloat n=screenWidth-20;
if (m<=n) {
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width +10;
}
else
{
y=y+stringsize.height+10;
x=20;
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width+10;
}
[btn setTitle:self.arrAssignUsers[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.tag=i;
[btn addTarget:self
action:#selector(notifyButtonPressedInEditTask:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor=WHITE_COLOR;
btn.layer.cornerRadius=10;
[btn.layer setMasksToBounds:YES];
btn.layer.borderWidth=2.0f;
btn.layer.borderColor=[UIColor orangeColor].CGColor;
[self checkNotifybtnStatus:btn];
[cell addSubview:btn];
}
}
It may helps to you.Thank you
You can use UICollectionView to create buttons. All you need to do is
1. Create custom cell with button as you want
2. When you tap on table view cell, add item to array and reload UICollectionView
3. Show buttons and it will automatically add scrollview for you.
When you tap on UICollectionView cell, remove object from array and reload UICollectionView again.
This will more easy as compared to adding buttons and setting frames.
And design UICollectionView cell in such way that it will same length as your text, use NSLayoutConstraints for it. It will work fast and smooth.
Code will be minimum and reusable.
Try out this working code...
Take UIScrollView in your Xib for buttons
//Adding menu buttons
btnArray = #[#"SYMPTOMATIC", #"SOCIAL", #"VOCATIONAL", #"PERSONAL"];
[self addButtonsInScrollMenu:btnArray];
/**
* Add Menu Buttons in Menu Scroll View
*
* #param buttonArray Array containing all menu button title
*/
#pragma mark - Add Menu Buttons in Menu Scroll View
-(void) addButtonsInScrollMenu:(NSArray *)buttonArray
{
CGFloat buttonHeight = self.menuScrollView.frame.size.height;
CGFloat cWidth = 0.0f;
for (int i = 0 ; i<buttonArray.count; i++)
{
NSString *tagTitle = [buttonArray objectAtIndex:i];
CGFloat buttonWidth = [self widthForMenuTitle:tagTitle buttonEdgeInsets:kDefaultEdgeInsets];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(cWidth, 0.0f, buttonWidth, buttonHeight);
[button setTitle:tagTitle forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"Roboto-Regular" size:12.0f];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.menuScrollView addSubview:button];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, button.frame.size.height - kButtonBorderHeight, button.frame.size.width, kButtonBorderHeight)];
bottomView.backgroundColor = [UIColor darkTextColor];
bottomView.tag = 1001;
[button addSubview:bottomView];
if (i == 0)
{
button.selected = YES;
[bottomView setHidden:NO];
}
else
{
[bottomView setHidden:YES];
}
cWidth += buttonWidth;
}
NSLog(#"scroll menu width->%f",cWidth);
self.menuScrollView.contentSize = CGSizeMake(cWidth, self.menuScrollView.frame.size.height);
}

How to make 3 clickable area on a button in Objective-C

I'm new in objective-C and I've create a project that use a button to change some elements from my view. I would like that depending on what part of the button I click, it displays different elements than previous.
For exemple when I click on the left area from button a red circle appears and when I click in the middle, it's the blue circle that appears and in the right area it's a green circle.
I tried searching on google and stackoverflow but I have trouble understanding how the selector used in this case. Could someone help me?
PS: Sorry for the mistakes, my English is not very good
I help you.
First you need the create the small custom view programmatically or through the xib or storyboard
UIView *viewButton = [[UIView alloc] initWithFrame: CGRectMake ( 100, 100, 300, 300)];
[self.view addSubView:viewButton];
Then Create the 3 buttons inside the viewButton
//First Button
UIButton *buttonLeftRed = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonLeftRed = [UIButton buttonWithType:UIButtonTypeSystem];
buttonLeftRed.frame = CGRectMake(0, 0, 100, 100);
buttonLeftRed.tag = 1;
[buttonLeftRed setTitle:#"Red" forState:UIControlStateNormal];
[buttonLeftRed addTarget:self
action:#selector(actionPressMeOne:) forControlEvents:UIControlEventTouchUpInside];
[viewButton addSubview:buttonLeftRed];
//Second Button
UIButton *buttonMiddleBlue = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonMiddleBlue = [UIButton buttonWithType:UIButtonTypeSystem];
buttonMiddleBlue.frame = CGRectMake(100, 0, 100, 100);
buttonMiddleBlue.tag = 2;
[buttonMiddleBlue setTitle:#"Blue" forState:UIControlStateNormal];
[buttonMiddleBlue addTarget:self
action:#selector(actionPressMeSecond:) forControlEvents:UIControlEventTouchUpInside];
[viewButton addSubview:buttonMiddleBlue];
//Third Button
UIButton *buttonRightGreen = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonRightGreen = [UIButton buttonWithType:UIButtonTypeSystem];
buttonRightGreen.frame = CGRectMake(200, 0, 100, 100);
buttonRightGreen.tag = 3;
[buttonRightGreen setTitle:#"Blue" forState:UIControlStateNormal];
[buttonRightGreen addTarget:self
action:#selector(actionPressMeThird:) forControlEvents:UIControlEventTouchUpInside];
[viewButton addSubview:buttonRightGreen];
If you want to change circle button, you have to import the Quartzcore Framework
#import <QuartzCore/QuartzCore.h>
#pragma mark - UIButton Action Methods
//First
- (void)actionPressMeOne:(UIButton *)sender
{
NSLog(#"Pressed Button Tag is - %#",sender.tag);
UIButton *button = sender;
//Then do whatever you want to do here
//half of the width
button.layer.cornerRadius = button.frame.size.width/2.0f;
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
button.clipsToBounds = YES;
[button setBackgroundColor:[UIColor redColor]];
}
//Second
- (void)actionPressMeSecond:(UIButton *)sender
{
NSLog(#"Pressed Button Tag is - %#",sender.tag);
UIButton *buttonTwo = sender;
//Then do whatever you want to do here
//half of the width
buttonTwo.layer.cornerRadius = button.frame.size.width/2.0f;
buttonTwo.layer.borderColor=[UIColor blueColor].CGColor;
buttonTwo.layer.borderWidth=2.0f;
buttonTwo.clipsToBounds = YES;
[buttonTwo setBackgroundColor:[UIColor blueColor]];
}
//Third
- (void)actionPressMeThird:(UIButton *)sender
{
NSLog(#"Pressed Button Tag is - %#",sender.tag);
UIButton *buttonThree = sender;
//Then do whatever you want to do here
//half of the width
buttonThree.layer.cornerRadius = button.frame.size.width/2.0f;
buttonThree.layer.borderColor=[UIColor greenColor].CGColor;
buttonThree.layer.borderWidth=2.0f;
buttonThree.clipsToBounds = YES;
[buttonThree setBackgroundColor:[UIColor greenColor]];
}
Thank You:-)

iOS UIButton can't click in UIScrollView

I had search some post before. But I can't try out the correct result.
I generate more button and add the view in the UIScrollView.
I had set autolayout in storybaord about the view and scrollview.
But the Button click are not listener. I don't know why.
I had set
_sclVW.delaysContentTouches = NO;
[_sclVW setCanCancelContentTouches:NO];
_sclVW.userInteractionEnabled = YES;
But the button still can't click.
My code is below:
_sclVW.delegate = self;
_sclVW.delaysContentTouches = NO;
[_sclVW setCanCancelContentTouches:NO];
_sclVW.userInteractionEnabled = YES;
int btnWidth = 80,btnHigh=50;
for( int i = 0 ; i < 50 ; i++ )
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(btnSendTag:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
button.tag = i;
button.titleLabel.numberOfLines = 0;
button.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.userInteractionEnabled = YES;
[button setEnabled:YES];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor darkGrayColor]];
if( i < 3)
{
button.frame = CGRectMake( i *(btnWidth + 20), 0 , 80,50 );
}
else
{
button.frame = CGRectMake( (i%3) *(btnWidth + 20), (i/3) *(btnHigh + 20) , 80,50 );
}
[_contentVW addSubview:button];
}
}
-(void) btnSendTag:(UIButton *)sender
{
NSLog(#"btn click:%li",(long)sender.tag);
}
Have anyone know how to resolve the problem?
thank you very much.
I had post my simple test project in github(this code).
I found your issue from sample project. Issue here is height and width of contentVW in Storyboard is 0. So that subview (buttons) of this view can not be interact with user.
Look at the picture below:
Here you can see that subview of UIScrollView has value 0 for width and height.
So change width and height of view same like scrollview and than you can able to tap on these buttons.

create horizontal scrolling UIButton in iOS using UIScrollView in ios 7

I'm new to ios development now want give a horizontal scrolling button in my app i dont no wt is the correct method to do it can any pls tell how to make it
this is the code i have tried to create a uibutton horizontal pro grammatically but its now working its throughing a warning
undeclared selector newView
i dont know wt i have to code in newview to make the button scroll im using more than four button i want all the buttons to be scroll in horizontal but i not able to make i have tried to create using one button
this is the code i have used in the view didload
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);
can any one pls tell me wt is the right way to make the horizontal scroll wheather i have use the uiscorllview and buttons in my storyboard and how to make into scroll the buttons
i have no idea to make im very confused by seening many tutorial sampel like thisone link
Follow my code:
CGFloat btnX = 80.0;
int numberOfButton = 10;
for (int i = 1 ; i <= numberOfButton; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchDown];
button.tag = i;
[button setTitle:#"Excavations" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[scrollview addSubView:button];
btnX = btnX + 165.0;
}
scrollview.contentSize = CGSizeMake(btnX + 50, yourHeight);
Your warning says that you forgot to add method of your UIButton, so you need to declare method of button, such like..
-(void) newView:(UIButton *) sender
{
//you can access your button by it's tag.
}
- (void)createScrollViewMenu
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
int x = 0;
for (int i = 0; i < 10; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];
[button setTitle:[NSString stringWithFormat:#"NameOfBtn %d", i] forState:UIControlStateNormal];
[button addTarget:self action:#selector(BtnClicked:) forControlEvents:UIControlEventTouchDown];
button.tag = i;
[scrollView addSubview:button];
x += button.frame.size.width;
}
scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
}
This will create a scrollview with a height of 100, width as big as its parent, and add 10 buttons to it.
so you need to declare method of button, such like..
-(void)BtnClicked:(UIButton *) sender
{
//you can access your button by it's tag.
}

Adding UIButton to subview not working

I'm trying to add 16 UIButton in code to a subview of my main view:
internView = [[UIView alloc] initWithFrame:CGRectMake(16, 16, (self.view.frame.size.width - 16), (self.view.frame.size.height - 16) - 60)];
internView.backgroundColor = [UIColor whiteColor];
for (int rij = 0; rij < 4; rij++) //4 rows
{
for (int kolom = 0; kolom < 4; kolom++) //4 colomns
{
CGFloat x = (((width + spacing) * kolom) + spacing);
CGFloat y = (((height + spacing) * rij) + spacing);
int tag = ((4 * rij) + kolom);
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor blackColor];
[button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[projects objectAtIndex:tag]thumbnailImage]]]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(getDetail:) forControlEvents:UIControlStateNormal];
button.tag = tag;
NSLog(#"%i",button.tag);
[internView addSubview:button];
}
}
[self.view addSubview:internView];
The subview 'internview' is visible (as i made it white background) on the main view but the buttons aren't visible.
Also my log shows the tag of the button which goes from 0..15, so I know they are being made but simple aren't added to the subview 'internview'
Can't really see what I'm missing or doing wrong here..
Change your code to :
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
In your version you have:
Memory leak because you have created buttons two times
[UIButton buttonWithType:UIButtonTypeCustom]; returns new button
with CGRectZero frame

Resources