iOS: Toggling between UIScrollView views without adding new views? - iphone

I am having a problem! When I use addSubview: and add a view to a UIScrollView, it adds a completely new view.
I want to either:
toggle between 3 predefined views, or
remove the last view from the UIScrollView before adding another one.
(All 3 views have some images so I'd imagine that the second option would be more efficient, memory-wise. However, perhaps the first option would be better on battery life? This is a 'subquestion'.)
How does one accomplish this?

You could accomplish your plan B by using removeFromSuperview function of your UIView.
Use it as below.
[mySecondImageView removeFromSuperview];
[myScrollView addSubview:myThirdImageView];
EDITED:
When you create UIImageView assign a tag value to each and also use and integer iVar to hold the tag value of your current view lets say it's currentViewTag ;
myFirstImageView.tag = 1;
mySecondImageView.tag = 2;
myThirdImageView.tag = 3;
currentViewTag = 1;
[myScrollView addSubview:myFirstImageView];
Now use as below
[[myScrollView viewWithTag:currentViewTag] removeFromSuperview];
[myScrollView addSubview:myThirdImageView];
currentViewTag = 3;

Sounds to me that you are trying to implement an infinite scrolling uiscrollview like the photos app.
If so, you don't need to remove any subviews and add them, you just need to create a UIScrollView that is as wide as three of your images and then when the scroll view is scrolled, change the image in either position 1 or 3 depending on which way the scroll is performed and then reset the position of the scroll view. If this is what you are trying to do, I can give more information if you need it.

Related

UIView Content Clipping Not Working/UIView Resize Not Working

I ran into an issue where clipping of subviews is not working consistently (or potentially, the UIView resize isn't working). Here's the scenario:
I have a UIView (section above the line). The actual size is larger to accommodate search bar. I basically resize it on viewDidLoad.
When clicking on the text field, UIView expands and show a search bar
After search, I collapse the UIView, but found that the UIView is not collapsed in certain case (see first image). If I were to not hide the search bar, it will remain also.
Is there any reason why this issue occurs? I'm still trying to debug and see if there is anything that could have caused this issue (as something may reset to original size). It definitely does seem like everything is resized correctly since even the result table is moved correctly also. Something else must have triggered it after the expected resize. Any pointer is appreciated. I've only done iOS development for 5 days so I'm still not aware of a lot of things.
- (void)showAddressField
{
CGRect headerFrame = self.searchHeaderView.frame;
headerFrame.size.height = headerHeight + adjustmentSize;
self.searchHeaderView.frame = headerFrame;
CGRect tableFrame = resultTableView.frame;
tableFrame.size.height = tableHeight - adjustmentSize;
tableFrame.origin.y = headerFrame.size.height + statusBarHeight;
resultTableView.frame = tableFrame;
[self renderHeaderBorder:headerFrame.size.height - 1];
}
- (void)hideAddressField
{
CGRect headerFrame = self.searchHeaderView.frame;
headerFrame.size.height = headerHeight;
self.searchHeaderView.frame = headerFrame;
CGRect tableFrame = resultTableView.frame;
tableFrame.size.height = tableHeight;
tableFrame.origin.y = headerHeight + statusBarHeight;
resultTableView.frame = tableFrame;
[self renderHeaderBorder:headerHeight - 1];
}
EDITED
SearchHeaderView is just a UIView that is a subview of the main view. It's not a table header. I found that if I put a search bar in the table header, it behaves very unpredictably so I have a UIView containing the search portion and have a UITableView right below it.
Sorry, since I only have just over a week to get a rather massive app out from scratch, I didn't have time to wait. I already changed my approach a little bit, but I will still award the points even after the bounty has expired. I'm trying to understand everything to do with layout since that's pretty much the only thing that I can't quite figure out with iOS app development.
If the search bar is in the table view header, try reassigning the header view to the table view:
[tableView setTableHeaderView:self.searchHeaderView];
If it is a section header, be sure to update the value for the delegate's
– tableView:heightForHeaderInSection:
– tableView:viewForHeaderInSection:
Otherwise, please post more code on how the searchHeaderView is initialized and added to the table.
Hope this helps!
Firstly, frames are already relative to the enclosing window. You should not be taking into account the height of the status bar (assuming that refers to the 20px area at the top of the screen).
I have found in the past that you may need to implement "layoutSubviews" in your view controller and compute the frames of the views there. It depends on your resizing mask / if you auto-layout enabled.
As the others have stated, more code would be helpful... or at least the relevant portions of the nib/xib.

UILabel with auto scroll

How I create UILabel with auto scroll from left to right?
like this http://www.youtube.com/watch?v=moI3ROPBm4Y
Thanks!
I use this code for this (From YouTube):
-(void)time:(NSTimer *)theTimer{
textLabel.center = CGPointMake(textLabel.center.x - 2.2, textLabel.center.y);
if (textLabel.center.x < -(textLabel.bounds.size.width/1.5)){
textLabel.center = CGPointMake(320 + (textLabel.bounds.size.width/1/5), textLabel.center.y);
}
how I to change the move label from left to right?
You can have a looping selector moving the viewarea of the frame slowly sideways. That should do the trick, without blocking the UI.
You would have to create a subclass of UILabel that overrides the drawRect. Then on a timer you would redraw the text with an offset, most easily with a ever increasing translation transform on the graphics context.
You also have to be careful to schedule the timer on the common modes because otherwise the scrolling of the label would stop as soon as the user starts scrolling any scroll view.

Adding Subviews programmatically vs. adding them in InterfaceBuilder

I have a beginner's question.
I was wondering about the level of subviews and how to determine them. Consider having put one button in IB (sampleButton) and then creating a subview programatically, like so:
[view insertSubview:aView atIndex:[view.subviews count]];
which is simply equivalent to:
[view addSubview:aView];
Now, in both cases, my index (which is referred to as z, right?) should be 1, if I am not mistaken.
I have one button added to my view in IB which should be at index 0 (z = 0, or do they start at one?). Then, if I do a view.subviews count, this will give me the number 1, as I have 1 object at index 0. So my new subview 'aView' will be placed at index = 1 which is just one layer above my sampleButton.
So far so good. But what if I did not want things to be this way? What if I wanted to be the sampleButton at a higher level (e.g. 1) and my 'aView' at the lowest (e.g. 0)? Can I simply do this:
[view insertSubview:aView atIndex:0];
Will this simply take care of my button created in IB, pushing it to another layer or will I have to tell IB that sampleButton shouldn't be at z=0?
It behaves as you already guessed: if you insert a subview at index 0 it is "below" all the other subviews.

can Any one suggest me how to begin with nested scroll view?

I need to keep a scrollview of size our view size,in that large scrollview again i have to keep six image views and six scrollviews.Please,can any one suggest something how to start with it.
Here is the key line of code...
[aView addSubview:nextView];

Using tags in iphone programming--can someone explain?

I've seen people using tags in iphone programming, like inside labels or tableview cells:
name.tag = kNameTag
Can someone explain with an example how these tags might be used? I gather that it's so that you can refer to a ui element later? Like if you programmatically use a for loop to create an array of UIButtons onto the iphone screen, do you assign tags to each button within the for loop or something?
Thanks!
The example you've included in your question is one of the common ones.
You can instantiate buttons (or other UI elements) in a loop, assigning a incremental tag to each. When an IBAction is invoked by one of those buttons, you can ask the sender for it's tag, which tells you exactly which button triggered the request.
for( int i = 0; i < 10; i++ ) {
UIButton * button = [[UIButton alloc] init...];
button.tag = i;
}
IBAction:
- (IBAction)doSomethingFromButtonTap:(id)sender {
NSLog(#"Button pressed: %d", [sender tag]);
}
They're also widely used to find specific subviews within a parent view. UIView provides a viewWithTag:(NSInteger)tag method. This is useful when building custom views without subclassing (or situations where you don't want to hold references to subviews, but know the tag).
Tags are integers. You assign them to a view using UIView.tag. You then use -[UIView viewWithTag:] to search the view hierarchy for the view.
UIKit doesn't use tags (I think), so they're free for you to use as necessary. However, tags are global to your app, so they're not an ideal replacement for IBOutlet (but it's often more convenient when you have a lot of views).
Avoid using 0 as a tag, since it's the default tag — [v viewWithTag:0] is unlikely to return the view you're looking for.