ios 8 - scroll does not work - ios

I am using UIScrollView . At run time I am adding UIButton into scrollview. In iOS 7 it work perfectly but in iOS 8 I am getting problem on scrolling (it is very hard to scroll )..
Here is my code . Please suggest me What am I doing wrong or in iOS 8 is there any new property? .
-(void)renderAllCatealogue:(NSArray *)catalogueArr
{
[[scollViewCatlog subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
int x=0;
for (NSDictionary *dict in catalogueArr)
{
UIView *view =[[UIView alloc]initWithFrame:CGRectMake(x, 8, 1, 16)];
[view setBackgroundColor:[UIColor whiteColor]];
[scollViewCatlog addSubview: view];
//calculate width
UIFont *font=[UIFont fontWithName:BOLD_FONT size:15];
CGFloat width=[[CommonUISetting sharedInstance]widthOfString:[dict valueForKey:#"catalogTitle"] withFont:font];
UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(x+10, 1, width, 30)];
[btn setTitle:[dict valueForKey:#"catalogTitle"] forState:UIControlStateNormal];
[btn setTag:[[dict valueForKey:#"catalogId"] integerValue]];
[btn.titleLabel setFont:font];
[btn addTarget:self action:#selector(catlogBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn.titleLabel setFont:[UIFont fontWithName:BOLD_FONT size:15]];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[scollViewCatlog addSubview:btn];
x=x+width +15;
}
[scollViewCatlog setContentSize:CGSizeMake(x, 1)];
}

i got solution .
I create a subclass of UIScrollview and add this method in it.
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}

I think you should use UICollectionView in order to generate something you want. UICollectionView allows to create vertical as well as horizontal infinite scroll panel. It is similar as the UITableView, you just have to setup some DataSource Methods and Delegate Methods.
look here for sample of UICollectionView

Related

Understanding the Aspect ratio constraint and multiplier

I have created a UIScrollview programmatically. And I have added some programmatically created buttons onto this scroll view. Button height is 30. But the button width changing according to the screen size.
float scrollYearX=btnLeftArrow.frame.origin.x+btnLeftArrow.frame.size.width+15;
float scrollYearY=40.0;
float scrollYearWidth=btnRightArrow.frame.origin.x-15-scrollYearX;
float scrollYearHeight=50.0;
scrollYear=[[UIScrollView alloc] initWithFrame:CGRectMake(scrollYearX, scrollYearY, scrollYearWidth, scrollYearHeight)];
[scrollYear setShowsHorizontalScrollIndicator:NO];
[scrollYear setShowsVerticalScrollIndicator:NO];
[vwParent addSubview:scrollYear];
int buttonX=0;
for (int i=0; i<100; i++) {
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(buttonX, 0, scrollYearWidth/5, 30)];
[btn.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
int mod=i%2;
if (mod==1) {
[btn setTitle:#"-" forState:UIControlStateNormal];
[btn.titleLabel setTextColor:[UIColor whiteColor]];
}
else
{
[btn addTarget:self action:#selector(yearClick :) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:[NSString stringWithFormat:#"%li",(long)backYear] forState:UIControlStateNormal];
if (backYear==year) {
btnCurrentYear=btn;
[btn setBackgroundImage:[UIImage imageNamed:#"yearSelect"] forState:UIControlStateNormal];
}
backYear++;
}
[scrollYear addSubview:btn];
btnLast=btn;
buttonX=buttonX+btn.frame.size.width;
}
[scrollYear setContentSize:CGSizeMake(btnLast.frame.origin.x+btnLast.frame.size.width, scrollYearHeight)];
[scrollYear setContentOffset:CGPointMake(btnCurrentYear.frame.origin.x-(btnLast.frame.size.width*2),0)];
My problem is which constraint I should use inorder to display this correctly in every device. Here I have attached how it shows on 6s and ipad pro. How can I make the ipad pro visual same as the 6s.
Button loks in 6s
Button looks in ipad pro
Please help me.
Thanks
This is because your button background width got scaled. you need to use resizable image to keep the round edge. Change your background image with follow:
UIImage* backgroundImage = [UIImage imageNamed:#"yearSelect"];
backgroundImage = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, backgroundImage.size.width/2-1, 0, backgroundImage.size.width/2)];
[btn setBackgroundImage:backgroundImage forState:UIControlStateNormal];

keep label text aligned center to screen while scrolling uilabel with view iOS?

I have a UITableView and 10 sections in it.
For each section I have a custom header.
In custom header I have a label as heading of section with text value date.
Table content size is (450,1000).
Here my label with text is also scrolling, but I want my label text should aligned center to screen and it should not scroll horizontally with table.
Thanks in advance.
After knocking my head for hours finally i got this solution.
Simply i used
(void)scrollViewDidScroll:(UIScrollView *)scrollView*
method and changed my label frame with table view content offset.
myLabel.frame = CGRectMake(tableview.contentoffset.x, 0, deviceHeight, 27);
It will keep my label at same position to screen, logically it is changing but will look like its stable at same position.
That behavior is only common when the UITableViewStyle property of the table is set to UITableViewStylePlain. If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells.
May be the following links will help you to solve your problem:
1. UITableView with fixed section headers
2. https://corecocoa.wordpress.com/2011/09/17/how-to-disable-floating-header-in-uitableview/
- (void)addButtonsOnScrollViewHorizontal:(int)postion :(UIScrollView*)scrollView
{
[scrollView setShowsVerticalScrollIndicator:NO];
float x = 30;
for (int i = 1; i <=postion; i++)
{
UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:i];
[button setTitle:[NSString stringWithFormat:#"%i",i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:26.0f/255.0f green:188.0f/255.0f blue:156.0f/255.0f alpha:100.0f] forState:UIControlStateSelected];
button.titleLabel.font =[UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0f];
CGSize maxSize = CGSizeMake(button.frame.size.width, FLT_MAX);
CGRect labRect = [button.titleLabel.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:button.titleLabel.font} context:nil];
[button setFrame:CGRectMake(x, 0, labRect.size.width+10, 30)];
[button addTarget:self action:#selector(scrollViewDaysbtnClicked:) forControlEvents:UIControlEventTouchUpInside];
if (i==postion)
{
[_scrollViewDays setContentOffset:CGPointMake (button.frame.origin.x+button.frame.size.width/2-70, 0) animated:NO];
button.titleLabel.font =[UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
[button setSelected:YES];
currentSelectionDays =i;
}
[scrollView addSubview:button];
x +=labRect.size.width+20;
}
scrollView .contentSize = CGSizeMake(x, 0);
}
-(void)scrollViewDaysbtnClicked:(UIButton*)sender
{
for (UIButton *button in _scrollViewDays.subviews)
{
if ([button isKindOfClass:[UIButton class]])
{
if (button.tag == sender.tag)
{
[_scrollViewDays setContentOffset:CGPointMake (button.frame.origin.x+button.frame.size.width/2-70, 0) animated:NO];
button.titleLabel.font =[UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
[button setSelected:YES];
currentSelectionDays =(int)sender.tag;
}else
{
[button setSelected:NO];
button.titleLabel.font =[UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0f];
}
}
}
}
use it....

How to add Dynamic elements in the reverse direction to a Uiscrollview in IOS?

I had a scrollview in my application with dynamic elements which i am adding from right to left like this
if(array.count>0)
{
[[cell.contentView viewWithTag:777] removeFromSuperview];
float x=315.0;
float width=0.0;
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(70, 52,245,20)];
scroll.backgroundColor=[UIColor clearColor];
scroll.tag=777;
[scroll setContentOffset:CGPointMake(70,245)];
scroll.showsHorizontalScrollIndicator=NO;
for(int i =0; i <[array count] ; i++)
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor colorWithRed:133.0/255.0 green:155.0/255.0 blue:179.0/255.0 alpha:1.0]];
btn.layer.cornerRadius = 2;
[btn setTitle:[[array objectAtIndex:i] objectForKey:#“ggg”] forState:UIControlStateNormal];
btn.userInteractionEnabled=NO;
[btn.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:10.0]];
CGSize size = [btn.titleLabel.text sizeWithAttributes:
#{NSFontAttributeName:
[UIFont fontWithName:#"Helvetica Neue" size:10.0]}];
x=x-(size.width+10);
//or whatever font you're using
[btn setFrame:CGRectMake(x,0,size.width+10,15)];
[scroll addSubview:btn];
width=width+x;
x=x-5;
}
scroll.contentSize=CGSizeMake((width+50),20);
// [scroll setPagingEnabled:YES];
[cell.contentView addSubview:scroll];
}
here everything working very fine,but i am not able to scroll to the last element added at the left.scroll was only allowing me to scroll up to 70 from left to right .Can anybody tell me where i am going wrong on this?
-(void)addButtonInScrollViewFromArray:(NSArray *)array{
NSInteger arrayCount=array.count;
NSInteger numberIteration=0;
CGFloat buttonXValue=300;
UIScrollView *buttonScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 360, 60)];
while (numberIteration<arrayCount) {
UIButton *buttonCreated=[UIButton buttonWithType:UIButtonTypeCustom];
buttonCreated.frame=CGRectMake(buttonXValue,5, 50, 50);
buttonXValue-=60;
[buttonCreated setTitle:[NSString stringWithFormat:#"Button%d",numberIteration] forState:UIControlStateNormal];
[buttonScrollView addSubview:buttonCreated];
numberIteration++;
}
CGFloat totalLength=numberIteration*60;
buttonScrollView.contentSize=CGSizeMake(totalLength,60);
}
this function you can pass the array
Can you try to below one
scroll.contentSize = CGSizeMake(x+size.width+10,20);

adding multiple subviews in ios

I'm trying to add multiple subview programmatically, but my buttons inside those subviews are not working. The buttons were also added as subviews programmatically.
Here's the code, hopefully will explain more about what I mean.
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(#"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:#"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
You need to increase the frame of the containerOfSelectedImage. Currently the width is 0.
The containerOfSelectedImage's frame has to be big enough so the button fits inside it. If the button is outside that frame it will show up but you can't touch him.
To debug this issues you can use a tool like Reveal App. If any of your buttons is outside the frame of the parent then the touch will not be detected.
Change [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; to [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

UIButton in TableViewCell VoiceOver Accessibility

SOLVED:
I followed the link in the update and now the UITableViewCell's buttons are working for VoiceOver.
The Situation:
I'm implementing VoiceOver accessibility for an app.
I have a custom UITableViewCell (code below) that can toggle between an expanded and a normal visual (selected vs unselected) by the user clicking the cell. Everything involved with expansion works according to my design.
In the custom UITableViewCell, I have a UILabel and 2 UIButtons.
The UILabel is recognized by the VoiceOver accessibility feature, but the UIButtons are ignored.
The Problem:
I need the VoiceOver Accessibility feature to recognize the UIButtons and read their accessibility labels and/or hints.
The Code:
-(id) init{
...
_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_leftButton.titleLabel setFont:[UIFont fontWithName:FONT size:12]];
[_rightButton.titleLabel setFont:[UIFont fontWithName:FONT size:12]];
[_leftButton addTarget:self action:#selector(clickButtonLeft:) forControlEvents:UIControlEventTouchUpInside];
[_rightButton addTarget:self action:#selector(clickButtonRight:) forControlEvents:UIControlEventTouchUpInside];
[_leftButton setFrame:CGRectMake(CELL_DETAILS_SIDE_PADDING,
CELL_PADDING + CELL_NORMAL_HEIGHT + CELL_EXPANDED_PADDING - CELL_BUTTON_HEIGHT-7,
70, CELL_BUTTON_HEIGHT)];
[_leftButton setImage:[UIImage imageNamed:#"giftIcon.png"] forState:UIControlStateNormal];
[_leftButton setTitle:#" Gift This" forState:UIControlStateNormal];
[_rightButton setFrame:CGRectMake(self.frame.size.width - 140 - CELL_DETAILS_SIDE_PADDING,
CELL_PADDING + CELL_NORMAL_HEIGHT + CELL_EXPANDED_PADDING - CELL_BUTTON_HEIGHT-7,
140, CELL_BUTTON_HEIGHT)];
[_rightButton setImage:[UIImage imageNamed:#"buyPack.png"] forState:UIControlStateNormal];
[_rightButton setTitle:#" Buy For Yourself" forState:UIControlStateNormal];
_description = [[UILabel alloc] initWithFrame:CGRectMake(CELL_DETAILS_SIDE_PADDING, CELL_PADDING+CELL_NORMAL_HEIGHT+10,
self.frame.size.width - 2*CELL_DETAILS_SIDE_PADDING, 50)];
_description.numberOfLines = 0;
[_description setLineBreakMode:NSLineBreakByWordWrapping];
[_description setFont:[UIFont fontWithName:FONT size:13]];
[_description setBackgroundColor:[UIColor clearColor]];
[_description setTextColor:[UIColor whiteColor]];
}
- (void) expandCell
{
...
[self addSubview:_description];
[self addSubview:_leftButton];
[self addSubview:_rightButton];
}
UPDATE:
It looks like what is described here is the way to go.
This other stack overflow question has the answer. Follow the tutorial in the answers.
stack overflow answer source

Resources