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.
Related
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);
}
Added 10 UIButtons in a UIScrollView. Wrote automatically scroll the scrollview code using UIAnimation.
for (int i=0; i<10; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(XPosition, YPosition, width,50);
[button setTitle:#"Warning animation items array " forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
if (index%2) {
button.backgroundColor = [UIColor blueColor];
} else{
button.backgroundColor = [UIColor purpleColor];
}
XPosition = paddingSpace + XPosition + button.frame.size.width;
[_scroll addSubview:button];
index++;
}
_scroll.contentSize=CGSizeMake(XPosition,50);
When animating the UIScrollView, I can't able tap any UIButton!!!
[UIView animateWithDuration:20
delay:0.0
options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat)
animations:^{
_scroll.contentOffset = CGPointMake(XPosition, 0.0);
} completion:^(BOOL finished){}];
If the animation is stop only I can tap the UIButton.
Please help me out this.
you can use scrollview.userintreactionenable=yes;
i think this help for you
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.
The scrollView is created in my storyboard and is horizontal. Here is the method where I add a button for every loop and need to increase the size of the scrollView every time a button is added.
- (void) populateGroups {
NSInteger x = [self.groupNameArray count];
for (int i = 0; i < x; i++) {
NSString *name = [self.groupNameArray objectAtIndex:i];
// Create Button
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(count, 0, 100, 54)];
[button setTitle:name forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:10];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor:[UIColor whiteColor].CGColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(groupPage:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.groupScrollView addSubview:button];
count = count + 100;
// Increase ScrollViewSize
self.groupScrollView.frame = CGRectMake(0, 0, frameWidth, 80);
frameWidth = frameWidth + 100;
}
}
The scrollView stays the same when I try to add two buttons to the view.
you have to increase the content size of the scroll view like:
self.groupScrollView.contentSize = CGSizeMake(frameWidth, 80);
and better to do it after the loop.
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.
}