I have a UIScrollView which scrolls only horizontally. I have a situation where I have views filled by array in UIScrollView. There are three views which altogether make one single component in UIScrollView (there are many such components). Component contains :
UILabel
UIButton
UIView (for underlying text)
I have placed two arrow keys(left and right) at the ends of UIScrollView. On touching left arrow I am shifting one position left (with complete string to display) in UIScrollView and one position right (with complete string to display) in case of right arrow button.
Till now I am able to perform left arrow action. But I have no idea how to perform right arrow touch and how to display complete string to touching arrow keys if only part of that string is being displayed.
I know it is weird that I have a UIScrollView and still want to add arrow keys to move it. But can't help it. This is client's requirement. The following is how I implemented left arrow action:
-(IBAction) onClickLeftArrow
{
NSLog(#"BUTTON VALUE : %i",iButtonValue);
if(iButtonValue <= [m_BCListArray count]) {
NSArray *arr = [scrollDemo subviews];
for(int j = 0; j < [arr count]; j++) {
UIView *view1 = [arr objectAtIndex:j];
[view1 removeFromSuperview];
}
XX = 5.0;
int tagcount = [m_BCListArray count]-iButtonValue;
NSLog(#"%#",m_BCListArray);
for(int i = iButtonValue; i >= 1; i--)
{
UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 120, 26)];
blabel.text = [m_BCListArray objectAtIndex:[m_BCListArray count]-i];
blabel.backgroundColor = [UIColor clearColor];
blabel.textColor = [UIColor whiteColor];
blabel.font = [UIFont systemFontOfSize:14];
[scrollDemo addSubview:blabel];
//underline code
CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:[m_BCListArray count]-1] sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame = blabel.frame;
newFrame.size.height = expectedLabelSize.height;
blabel.frame = CGRectMake(blabel.frame.origin.x, blabel.frame.origin.y, expectedLabelSize.width+10, expectedLabelSize.height);
blabel.numberOfLines = 1;
[blabel sizeToFit];
int width=blabel.bounds.size.width;
int height=blabel.bounds.size.height;
UIButton *btnContent = [UIButton buttonWithType:UIButtonTypeCustom];
[btnContent addTarget:self action:#selector(SelectButton:)forControlEvents:UIControlEventTouchDown];
btnContent.tag = tagcount+1;
btnContent.frame=CGRectMake(XX, 2.0, width+10, height);
[scrollDemo addSubview:btnContent];
scrollDemo.contentSize = CGSizeMake(XX+50, 32);
//scrollDemo.contentSize = CGSizeMake(expectedLabelSize.width,expectedLabelSize.height);
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake(XX, 26, width, 1);
viewUnderline.backgroundColor=[UIColor whiteColor];
[scrollDemo addSubview:viewUnderline];
[viewUnderline release];
scrollDemo.contentSize = CGSizeMake(XX+width+10, 32);
XX = XX + width+10;
iRight = iRight + 1;
tagcount ++;
// iRight --;
}
}
iButtonValue = iButtonValue+1;
}
Set the contentOffset of UIScrollView on the Button click action and u'll get your desired effect.
Related
I have 7 UIButtons in a UIScrollView. The top 3 UIButtons can be either hidden or visible depending on if statements in code. Meaning there are 8 possibilities of the 3 UIButtons displaying.
The problem i'm having is getting the UIScrollView to adjust its content size so that there are no "empty" looking spaces in it. For example if the middle UIButton is hidden but the ones above and below it are visible, there is an empty space left.
Is there any way make all the buttons fit one underneath the other in the scrollview without any gaps?
I have tried this code but it didn't work:
if([[facilities objectAtIndex:0]intValue] == 1) {
facilitiesButton.hidden = NO;
}
if([[ListingsEnabled objectAtIndex:0]intValue]==1) {
ListingsBtn.hidden = NO;
}
if([[OffersEnabled objectAtIndex:0]intValue]==1) {
OffersBtn.hidden = NO;
}
CGFloat scrollViewHeight = 0.0f;
self.DetailScrollView.showsHorizontalScrollIndicator = NO;
self.DetailScrollView.showsVerticalScrollIndicator = NO;
for (UIView* view in self.DetailScrollView.subviews)
{
if (!view.hidden)
{
CGFloat y = view.frame.origin.y;
CGFloat h = view.frame.size.height;
if (y + h > scrollViewHeight)
{
scrollViewHeight = h + y;
}
}
}
self.DetailScrollView.showsHorizontalScrollIndicator = YES;
self.DetailScrollView.showsVerticalScrollIndicator = YES;
[self.DetailScrollView setContentSize:(CGSizeMake(self.DetailScrollView.frame.size.width, scrollViewHeight))];
as #Andy commented, you need to position the actual buttons, not just change the scroll view size. A Table View may be indeed the way to go, but for 7 buttons that may be overkill. Here's a snippet that will loop through the buttons and update their positions based on visibility. I omitted the scroll view because it doesn't seem necessary, but if you need them in a scroll view that will update i can edit the code, just let me know.
/////////////////////
///// USES ARC //////
/////////////////////
- (void)viewDidLoad
{
[super viewDidLoad];
allButtons = [NSMutableArray array];
// Create 7 buttons and randomly hide some
for (int i = 0; i < 7; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 50);
[button setTitle:[NSString stringWithFormat:#"Button %i", i+1] forState:UIControlStateNormal];
[button addTarget:self action:#selector(randomizeButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
button.hidden = arc4random_uniform(2)-1 > 0;
[allButtons addObject:button];
}
[self updateButtonLayout];
}
-(void)updateButtonLayout
{
// position buttons based on visiblity
CGRect lastButtonFrame = CGRectMake(0, 0, 0, 0);
int verticalPadding = 10;
for(UIButton *btn in allButtons)
{
if(!btn.hidden)
{
btn.frame = CGRectMake(lastButtonFrame.origin.x, lastButtonFrame.origin.y+lastButtonFrame.size.height+verticalPadding, btn.frame.size.width, btn.frame.size.height);
lastButtonFrame = btn.frame;
}
}
}
-(void)randomizeButtons:(UIButton *)btn
{
// Randomize the visiblity of the buttons and update the interface
for(UIButton *btn in allButtons)
{
btn.hidden = arc4random_uniform(2)-1 > 0;
}
[self updateButtonLayout];
}
Please read my question before marking it as duplicate or repeat question...
I have one scroleView in which I put some images in tabular form.
When user click one of it the next view controller is shown and the
clicked image's uiview get green border.
And when user navigate back to this view the clicked image is shown
with green border. all this worked fine
But the problem starts when user clicks other images : the previously clicked image doesn't get back to normal, ie, its border stays there even if I put its width to 0.0 and color to clearColor
Please guide me how to remove those borders
My code is as below:
for (int row = 0; row < r; ++row)
{
for (int col = 0; col < 2; ++col)
{
int index = (row * 2) + col;
if(index < [tempArr count])
{
CGRect frame = CGRectMake(10+col*(10+145),10+row*(5+100),145, 100);
UIView *fr = [[UIView alloc] initWithFrame:frame];
CGRect imgFrame = CGRectMake(0, 0, 145, 100);
UIImageView *imgView = [[UIImageView alloc]initWithFrame:imgFrame];
imgView.image = [UIImage imageNamed:[[tempArr objectAtIndex:index]valueForKey:#"add"]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapAction:)];
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
if(selctedFrame == index)//Here i put border
{
fr.layer.borderWidth = 2.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
}
else //here i remove them
{
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor clearColor].CGColor;
}
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[fr addGestureRecognizer:tapGesture];
[fr addSubview:imgView];
fr.tag = index;
[self.scrollDisplay addSubview:fr];
[self.scrollDisplay bringSubviewToFront:fr];
}
}
}
[self.view addSubview:self.scrollDisplay];
This method is called in viewWillAppear:animated: method
Edit
after some navigation forth and back
New Answer- I think the problem is that you're adding new views again and again. Instead of doing UIView *fr = [[UIView alloc] initWithFrame:frame];, find the already existing view as: UIView *fr = [self.scrollDisplay viewWithTag:index]; and make changes to it. In effect, new imageviews are being added over the old ones which is highly inefficient, besides causing the mentioned issue. I'm assuming you've already added the views to scrollDisplay. You also shouldn't create new imgView objects again. Set tags for them that make them unique and easy to get. For eg, set the tag 999 for each imgView and retrieve it as:UIImageView *imgView = [fr viewWithTag:999]; Also, get rid of [fr addSubview:imgView];, [self.scrollDisplay addSubview:fr]; and [self.view addSubview:self.scrollDisplay]; towards the end. So your code should look like this:
for (int row = 0; row < r; ++row)
{
for (int col = 0; col < 2; ++col)
{
int index = (row * 2) + col;
if(index < [tempArr count])
{
CGRect frame = CGRectMake(10+col*(10+145),10+row*(5+100),145, 100);
UIView *fr = [self. scrollDisplay viewWithTag:index];
CGRect imgFrame = CGRectMake(0, 0, 145, 100);
UIImageView *imgView = [fr viewWithTag:999];
//imgView.image = [UIImage imageNamed:[[tempArr objectAtIndex:index]valueForKey:#"add"]]; //<--You've already set the image before so you don't need this
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapAction:)];
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
if(selctedFrame == index)//Here i put border
{
fr.layer.borderWidth = 2.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
}
else //here i remove them
{
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor clearColor].CGColor;
}
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
//[fr addGestureRecognizer:tapGesture]; //<-- Not needed if you've already done this
//[fr addSubview:imgView];//<-- Not needed
fr.tag = index;
//[self.scrollDisplay addSubview:fr];//<-- Not needed
[self.scrollDisplay bringSubviewToFront:fr];//<-- probably not needed
}
}
}
//[self.view addSubview:self.scrollDisplay];//<-- Not needed
i just need to do is to set the border color to the background color of the view. any other options did nothing the border is not been hidden through any thing else the only work around i got is this
I have task to develop the application with words counting and display in runtime generated uiscrollview with uilabel as subview.
The process is like when user will load page at that time the 1000 of word will fill in the rutime generated uilabel with scrollview.And all thing is set from runtime. but it the application we have one button to add the current uilabel text on button click.
and the words are coming random from database. when i click on the button it gives me the different word. from scroll view displayed word.
Following is my code to fill the data in uilabel with scrollview :
wordName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 2)];
[wordNameArray addObject:wordName];
int i = 0;
for (i = 1; i <= kNumImages; i++)
{
randomN = [wordNameArray objectAtIndex:arc4random() % [wordNameArray count]];
UIImage *image = [UIImage imageNamed:#"word_bg.png"];
word = [[UILabel alloc]initWithFrame:CGRectMake(15,12,250,120)];
[word setFont:[UIFont fontWithName:#"Helvetica-Bold" size:36.0f]];
word.textColor = [UIColor whiteColor];
word.lineBreakMode = UILineBreakModeWordWrap;
word.numberOfLines = 2;
word.textAlignment = UITextAlignmentCenter;
word.backgroundColor = [UIColor clearColor];
[word setText:randomN];
lblWord.text = word.text;
word.tag = i;
NSLog(#"tag no:%d",word.tag);
imageView = [[UIImageView alloc] initWithImage:image];
[imageView addSubview:word];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
//NSLog(#"%#",word.text);
//NSLog(#"%#",lblWord.text);
// [scrollView1 addSubview:l];
[scrollView1 addSubview:imageView];
touchToSee.hidden = TRUE;
[imageView release];
}
[self layoutScrollImages];
Following is my scroll event to get the word from label but it is giving me last word from array:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
touchToSee.hidden = TRUE;
NSLog(#"word-%#",word.text);
}
And other when i click on button i am not getting the perfect work so for that following is my code:
NSString *getword = [NSString stringWithFormat:#"%#",word.text];
NSLog(#"lblCountdownBackward:%#",word.text);
So please help me and provide me some sample code for it if it possible.
thanks in advance.
You appear to be creating lots of word objects, and adding them to the screen.
In your for loop, you perform your code upto kNumImages. And in each run, the word Object is replace with a new one.
So when you get the word later on, it will be the very last word object you created (hence the last word).
To get arround this you could use an NSMutableArray and keep adding a new UILabel object to that (instead of creating the word object). And then later on do something like
[array objectAtIndex:0].text;
I have a sprite on which there is a label (CCLabelTTF). On that label i have A,B,C,D,E, etc printed when they are clicked. I want them to scroll left. I've googled some tuorials but i am unable to find the solution and stuck here for long. Here is the screenshot of my game. You can see the characters from A to J. When i click on more incoming characters, that portion should scroll. What can i do to make the characters scroll?
Here is the code from which the characters are shown on label (lblSelectedAlpha) added on sprite:-
-(void)showSelectedAlphaBet{
fSelectedAlphaY =26;
if (tempBackground) {
[tempBackground removeFromParentAndCleanup:YES];
}
tempBackground = [CCSprite spriteWithFile:#"stripe_2.png"];
tempBackground.position = ccp(160,30);
[self addChild:tempBackground z:30];
for (int i=0; i<[arryAddSelectedChar count]; i++) {
// NSLog(#"[arryAddSelectedChar count] %#",[arryAddSelectedChar objectAtIndex:i]);
lblSelectedAlpha = [CCLabelTTF labelWithString:
[arryAddSelectedChar objectAtIndex:i]dimensions:CGSizeMake(30,30)
alignment:UITextAlignmentCenter fontName:#"Marker Felt" fontSize:30];
lblSelectedAlpha.position = ccp(fSelectedAlphaY,25);
lblSelectedAlpha.tag=i;
lblSelectedAlpha.color = ccc3(125,125,125);
[tempBackground addChild:lblSelectedAlpha z:5];
fSelectedAlphaY +=25;
}
}
Scrolling is simply a constant change in position over time. Basically something like this:
-(void) update:(ccTime)delta
{
label.position = CGPointMake(label.position.x - 1, label.position.y);
}
Well, this is how i implemented scrolling in my cocos2d game
// How to add scroll on label. This i did with a UITextView
// inside init, i took
{
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10,430, 150, 50)]
alphabetScroll = [[UITextView alloc] initWithFrame:CGRectMake(10,0,50 ,50)];
alphabetScroll.font = [UIFont fontWithName:#"verdana" size:30.0f];
alphabetScroll.backgroundColor = [UIColor clearColor];
alphabetScroll.textColor = [UIColor colorWithRed:125.0/255.0 green:125.0/255.0 blue:125.0/255.0 alpha:1.0f];
alphabetScroll.userInteractionEnabled=NO;
[scroll addSubview:alphabetScroll];
[[[CCDirector sharedDirector]openGLView]addSubview:scroll];
}
-(void)showSelectedAlphaBet
{
NSLog(#"showSelectedAlphaBet");
fSelectedAlphaY =26; // I have 26 alphabets
if (tempBackground) {
[tempBackground removeFromParentAndCleanup:YES];
}
// Area where alphabets are added on touch
tempBackground = [CCSprite spriteWithFile:#"stripe_2.png"];
tempBackground.position = ccp(160,30);
[self addChild:tempBackground z:30];
bottomAlphabet = #" ";
for (int i=0; i<[arryAddSelectedChar count]; i++) {
NSLog(#"[arryAddSelectedChar count] %#",[arryAddSelectedChar objectAtIndex:i]);
bottomAlphabet = [bottomAlphabet stringByAppendingString:[arryAddSelectedChar objectAtIndex:i]];
}
// Implementing shifting/scrolling
int newScrollViewWidth;
int newLabelWidth;
newScrollViewWidth =25*[arryAddSelectedChar count];
newLabelWidth =50*[arryAddSelectedChar count];
[scroll setContentSize:CGSizeMake(newScrollViewWidth, 45)];
alphabetScroll.frame = CGRectMake(0, 0, newLabelWidth, 45);
alphabetScroll.text = bottomAlphabet;
if (newScrollViewWidth > 150) {
CGPoint svos;
CGPoint pt;
svos = scroll.contentOffset;
CGRect rc = [alphabetScroll bounds];
rc = [alphabetScroll convertRect:rc toView:scroll];
pt = rc.origin;
pt.y = 0;
pt.x += 20*([arryAddSelectedChar count]-5);
[scroll setContentOffset:pt animated:YES];
}
}
// In dealloc
[alphabetScroll release];
I have ScrollView and UIView created in interface builder (but can as well be code). I want to add random amount of thumbnails (subviews) to UIView and be able to scroll it up-down if there are more than the screen can take. Hovewer, I can't get ScrollView to scroll.
Where and how do I resize UIView and add it as a subview of ScrollView?
When do I set contentSize of scrollView to make it expand along with uiview? I tried creating fixed large contentSize but didn't work as well.
What properties in IB I might need to change to make it work?
I'd like to make the images in uiview clickable in next step.
I guess I could also make both view in code without IB. Just somehow can't get it to work.
Thanks in advance !
//declare base view
UIViewController *viewForLoadForm =[[UIViewController alloc] init];
viewForLoadForm.view.frame=CGRectMake(0, 0,screenSize.size.width,screenSize.size.height);
viewForLoadForm.view.backgroundColor=[UIColor blackColor];
[Manview.view addSubview:viewForLoadForm];
//declare scrollview and add to base view
UIScrollView *scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenSize.size.width,screenSize.size.height)];
scrollview.indicatorStyle=UIScrollViewIndicatorStyleBlack;
[scrollview setContentSize:CGSizeMake(screenSize.size.width,screenSize.size.height)];
scrollview.clipsToBounds = NO;
scrollview.scrollEnabled = YES;
scrollview.pagingEnabled = NO; scrollview.showsVerticalScrollIndicator =NO;
scrollview.alwaysBounceVertical= YES;
[viewForLoadForm.view addSubview:scrollview];
//Add your controls to scrollview that s image view or something which is needed to display
.........your control's code (textbox,imageview etc)
//took last control's y postion and set your K
float k=Last control's y position +100;(some thing which ll be decide scroll size)
// reassign the scrollview height
[scrollview setContentSize:CGSizeMake(screenSize.size.width,k)];
use above line for scroll view size adjustment at run time.
screenSize.size.width is 320 here
code snippet,
- (void) createThumbView
{
float y_axis = Set y axis;
int x = 0;
int totalImgs = total images;
int tempCnt = 0;
for (int i = 0; i < totalImgs;)
{
float x_axis = set x axis;
int loopCount = 0;
if (totalImgs - tempCnt >= (no of images in row))
{
loopCount = (no of images in row);
}
else
{
loopCount = totalImgs % (no of images in row);
}
for (int j = 0; j < loopCount; j++)
{
MasksData *mData = [masksList objectAtIndex:x];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x_axis, y_axis, width, height);
[btn setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png",mData.bgImage]] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
btn.tag = x;
[scroll View addSubview:btn];
//photoCount++;
x_axis += width + some space;
tempCnt++;
i++;
x++;
}
y_axis += height + some space;
scroll View.contentSize = CGSizeMake(320.0, y_axis);
}
}