Bizarre UIScrollView behavior - iphone

I've got myself totally baffled with this scrollview. For some reason, on the third pass of this loop, an extra two UIImageViews get added as subviews. I assume i'm addressing bad memory, or something to that effect, but I can't figure out where it is coming from for the life of me.
Here's the code:
scrollview=[[[UIScrollView alloc] initWithFrame:CGRectMake(x, y+40, 300, 225)] retain];
NSArray *chunks=[lesson.photoString componentsSeparatedByString:#"|"];
ALAssetsLibrary *assetLibrary;
__block CGRect workingFrame = scrollview.bounds;
__block UIImageView *imageview=nil;
workingFrame.origin.x = 0;
NSLog(#"Chunks: %d\n",[chunks count]);
for(NSString *url in chunks) {
//
if(url && [url length])
{
NSURL *asseturl = [NSURL URLWithString:url];
assetLibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetLibrary assetForURL:asseturl
resultBlock:^(ALAsset *myasset)
{
UIView *sub=[[UIView alloc] initWithFrame:workingFrame];
imageview=[[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:[[myasset defaultRepresentation] fullScreenImage]]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = sub.bounds;
[sub addSubview:imageview];
[imageview release];
UIButton *imgfull=[UIButton buttonWithType:UIButtonTypeCustom];
[imgfull setFrame:sub.bounds];
imgfull.backgroundColor=[UIColor clearColor];
[imgfull addTarget:self action:#selector(imageViewToFullScreen) forControlEvents:UIControlEventTouchUpInside];
[sub addSubview:imgfull];
[imgfull release];
[scrollview addSubview: sub];
[sub release];
NSLog(#"%#",[scrollview subviews]);
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
[scrollview setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
if(scrollview.contentSize.width>300)
[scrollview flashScrollIndicators];
}
failureBlock:^(NSError *myerror){
NSLog(#"Failure - %#",[myerror localizedDescription]);
}
];
}
}
[scrollview setPagingEnabled:YES];
[self.view addSubview:scrollview];
Here's the output:
2011-05-04 11:53:00.331 AppName[24896:207] Chunks: 4
2011-05-04 11:53:00.369 AppName[24896:207] (
"<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>"
)
2011-05-04 11:53:00.394 AppName[24896:207] (
"<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>",
"<UIView: 0x642db00; frame = (300 0; 300 225); layer = <CALayer: 0x6420c80>>"
)
2011-05-04 11:53:00.426 AppName[24896:207] (
"<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>",
"<UIView: 0x642db00; frame = (300 0; 300 225); layer = <CALayer: 0x6420c80>>",
"<UIImageView: 0x666e3d0; frame = (292 1; 7 223); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x666e400>>",
"<UIImageView: 0x6605490; frame = (1 217; 149 7); opaque = NO; autoresize = TM; userInteractionEnabled = NO; animations = { opacity=<CABasicAnimation: 0x6686ff0>; }; layer = <CALayer: 0x6664280>>",
"<UIView: 0x6686c30; frame = (600 0; 300 225); layer = <CALayer: 0x6686c60>>"
)
2011-05-04 11:53:00.439 AppName[24896:207] (
"<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>",
"<UIView: 0x642db00; frame = (300 0; 300 225); layer = <CALayer: 0x6420c80>>",
"<UIImageView: 0x666e3d0; frame = (292 1; 7 223); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x666e400>>",
"<UIView: 0x6686c30; frame = (600 0; 300 225); layer = <CALayer: 0x6686c60>>",
"<UIImageView: 0x6605490; frame = (1 217; 99 7); opaque = NO; autoresize = TM; userInteractionEnabled = NO; animations = { opacity=<CABasicAnimation: 0x6648d60>; }; layer = <CALayer: 0x6664280>>",
"<UIView: 0x6673f90; frame = (900 0; 300 225); layer = <CALayer: 0x6673fc0>>"
)
EDIT: I should also mention that I've NSLog'd sub, and it produces the results I'd expect (1 imageview, 1 button per trip through the loop). I should also mention, that as far as I know I have no imageviews of the dimensions shown from the scrollview logging (99x7 or 7x223) anywhere in my app.
EDIT 2: A bit further information: I switched the add line to [scrollview addSubview:nil]; and I'm still seeing two imageviews being added on the third pass of the loop. Also, scrollview isn't accessed from anywhere else outside the posted code section.
EDIT 3: Figured out out. I'm flashing the scrollbars too early, which is mucking with the order of the subviews within the scrollview.

Are these the scroll view indicators flashing?

If no unusual images actually appear on your scroll view, you can assume those subviews are managed internally by UIScrollView and you should ignore them. Scroll indicators, perhaps.

Actually These extra two images are the uiscrollindicator images that are vertical scroll indicator and horizontal scroll indicator with tag 0 That are added by UIScrollview Automatically , if you add UIIMageViews to the ScrollView make sure to assign tag greater than zero, or use some other technique , so that you can retrieve your UIIMageViews by tag greater than zero..

Related

Can we set custom image for scrollbar of UITableview?

I want to set my custom image for UITableView scrollbar.
For that i found this link: WKVerticalScrollBar for iOS
But it seems too complex to implement. It is just an image for TableView scrollbar.
Is there any other easy way to do that?
UPDATE - 1
My Tableview has only these 2 subviews
"<UIImageView: 0x6c2c9f0; frame = (0 778; 360 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x6c2ca60>>",
"<UIImageView: 0x6c06680; frame = (353 778; 7 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x6c2be70>>"
With that i Have tried with both index but any of it not working
tblvw.showsVerticalScrollIndicator = YES;
NSLog(#"tblview subview : %#",tblvw.subviews);
UIImageView *imgview = [tblvw.subviews objectAtIndex:0];
imgview.image = [UIImage imageNamed:#"Slider-strip.png"];
After making tblvw.showsVerticalScrollIndicator = NO;
I came to know that i was not getting the 2nd imageview as subview & only 1st imageview. So i tried using [tblvw.subviews objectAtIndex:1]; but also not working
Working Perfectly:
EDIT : Note: Best implementation is to have Vertical Image. For example like.............................................
if([self.tableView.subviews count] > 2){
UIImageView *scrollViewBarImgView = [self.tableView.subviews objectAtIndex:2];
UIImage *imgBar = [UIImage imageNamed:#"image.png"]; //your image here
[scrollViewBarImgView setImage:imgBar];
}
else{
UIImageView *scrollViewBarImgView = [self.tableView.subviews objectAtIndex:1];
UIImage *imgBar = [UIImage imageNamed:#"image.png"]; //your image here
[scrollViewBarImgView setImage:imgBar];
}

IOS activityIndicator subview above MKMapView

I have a mapView (_mapView) which in itself is a subView of my viewController , I would like to add an activityIndicator when I am fetching lots of pins, however I cannot get it to appear above the mapView, can anyone suggest why not
UIActivityIndicatorView *activityIndicator = nil;
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] ;
CGRect frame = activityIndicator.frame;
frame.origin = CGPointMake(290, 12);
activityIndicator.frame = frame;
activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
activityIndicator.tag = 2;
[self._mapView addSubview:activityIndicator];
dataMarkers = [[kepnService new] getPlacesByBounds:neCoord.latitude :neCoord.longitude :swCoord.latitude :swCoord.longitude]; // call to webservice
.... other processing code
[activityIndicator removeFromSuperview];
For information my view stack looks like this
subviews (
"<MKMapView: 0x6d6f320; frame = (0 0; 320 460); clipsToBounds = YES; layer = <CALayer: 0x6d99520>>",
"<UIToolbar: 0x6d93f70; frame = (-1 0; 342 44); autoresize = W+TM; layer = <CALayer: 0x6d8bbf0>>",
"<UINavigationBar: 0x6d9a4f0; frame = (0 416; 320 44); autoresize = W+BM; layer = <CALayer: 0x6d9a5b0>>"
)
Seems like you are adding the activity indicator, blocking the main thread and then removing it.
What you should do is:
Add the activity indicator.
Detach a thread or block to the background to perform the processing code.
When done, remove the activity indicator.

UIScrollView adding UIImageViews

I want to add n number of views to a scroll view with paging enabled.
if hit the "add view" button say 5 times, all 5 get added w/out uiimageviews. but if I swipe to the 2nd view and click "add view" again, 2 UIImageViews get added to the scroll view (subviews). why are these image views being added after I swipe to the next view?
here I set up the view:
- (void)viewDidLoad
{
CGRect myRect = CGRectMake(0, 150, ([[UIScreen mainScreen] bounds].size.width), ([[UIScreen mainScreen] bounds].size.height - (64 + 150)));
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:myRect];
myScrollView.backgroundColor = [UIColor lightGrayColor];
myScrollView.pagingEnabled = TRUE;
myScrollView.showsHorizontalScrollIndicator = TRUE;
myRect = CGRectMake(20, 75, 150, 13);
myRect = CGRectMake(400, 50, 100, 50);
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(400, 50, 100, 50);
myButton.titleLabel.text = #"get views";
[myButton addTarget:self action:#selector(makePlantEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
myRect = CGRectMake(0, 0, (768/2), (1004/2));
[self.view addSubview:myScrollView];
self.mainScrollView = myScrollView;
[super viewDidLoad];
}
and here is my method for adding views:
-(IBAction)makePlantEvent:(id)sender
{
NSLog(#"number of views in scrollview: %d", [self.mainScrollView.subviews count]);
NSLog(#"views in scrollView: %#", self.mainScrollView.subviews);
if ([self.mainScrollView.subviews count] == 0)
{
NSLog(#"adding first view");
CGRect myRect = CGRectMake(10, 10, self.mainScrollView.frame.size.width - 20, self.mainScrollView.frame.size.height - 20);
UIView *myUIView = [[UIView alloc] initWithFrame:myRect];
myUIView.backgroundColor = [UIColor whiteColor];
myUIView.layer.borderWidth = 1.0f;
myUIView.layer.borderColor = [UIColor blackColor].CGColor;
self.mainScrollView.contentSize = CGSizeMake(768, mainScrollView.frame.size.height);
[self.mainScrollView addSubview:myUIView];
}
else if ([self.mainScrollView.subviews count] >= 1)
{
NSLog(#"adding %d view", ([self.mainScrollView.subviews count] + 1));
self.mainScrollView.contentSize = CGSizeMake((768 * ([self.mainScrollView.subviews count] + 1)), mainScrollView.frame.size.height);
CGRect myRect = CGRectMake((768 * [self.mainScrollView.subviews count]) + 10, 10, self.mainScrollView.frame.size.width - 20, self.mainScrollView.frame.size.height - 20);
UIView *myUIView = [[UIView alloc] initWithFrame:myRect];
myUIView.backgroundColor = [UIColor blueColor];
myUIView.layer.borderWidth = 1.0f;
myUIView.layer.borderColor = [UIColor blackColor].CGColor;
[self.mainScrollView addSubview:myUIView];
}
NSLog(#"at end number of views in scrollview: %d", [self.mainScrollView.subviews count]);
NSLog(#"at end views in scrollView: %#", self.mainScrollView.subviews);
}
my output:
add 2 views
2011-11-28 13:42:39.976 Inspect[87637:207] BEGINING OF METHOD number of views in scrollview: 0
2011-11-28 13:42:39.977 Inspect[87637:207] BEGINING OF METHODviews in scrollView: (
)
2011-11-28 13:42:39.978 Inspect[87637:207] adding first view
2011-11-28 13:42:39.978 Inspect[87637:207] at end number of views in scrollview: 1
2011-11-28 13:42:39.979 Inspect[87637:207] at end views in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>"
)
2011-11-28 13:42:41.652 Inspect[87637:207] BEGINING OF METHOD number of views in scrollview: 1
2011-11-28 13:42:41.653 Inspect[87637:207] BEGINING OF METHODviews in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>"
)
2011-11-28 13:42:41.654 Inspect[87637:207] adding 2 view
2011-11-28 13:42:41.654 Inspect[87637:207] at end number of views in scrollview: 2
2011-11-28 13:42:41.655 Inspect[87637:207] at end views in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>",
"<UIView: 0x8a1f390; frame = (778 10; 748 790); layer = <CALayer: 0x8a26d70>>"
)
swipe to 2nd view and click button again:
2011-11-28 13:42:49.628 Inspect[87637:207] BEGINING OF METHOD number of views in scrollview: 4
2011-11-28 13:42:49.629 Inspect[87637:207] BEGINING OF METHODviews in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>",
"<UIView: 0x8a1f390; frame = (778 10; 748 790); layer = <CALayer: 0x8a26d70>>",
"<UIImageView: 0x108016d0; frame = (760 1; 7 808); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x10800200>>",
"<UIImageView: 0x10801690; frame = (1152 802; 383 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x108000a0>>"
)
2011-11-28 13:42:49.630 Inspect[87637:207] adding 3 view
2011-11-28 13:42:49.630 Inspect[87637:207] at end number of views in scrollview: 5
2011-11-28 13:42:49.631 Inspect[87637:207] at end views in scrollView: (
"<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>",
"<UIView: 0x8a1f390; frame = (778 10; 748 790); layer = <CALayer: 0x8a26d70>>",
"<UIImageView: 0x108016d0; frame = (760 1; 7 808); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x10800200>>",
"<UIImageView: 0x10801690; frame = (1152 802; 383 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x108000a0>>",
"<UIView: 0xf4002f0; frame = (3082 10; 748 790); layer = <CALayer: 0xf4001c0>>"
so the swipe adds the two UIImageViews. Why?
There are scroll indicators. When your scrollview contentSize > scrollview size then UIScrollView automatically adds two subviews (UIImageView) to itself.
To disable this feature set showsHorizontalScrollIndicator and showsVerticalScrollIndicator properties to NO.

Where Does A UIAlertView Live While Not Dismissed

Does anyone know in whose subview an active UIAlertView is located or how to find the thread in which it is running?
If you dump the contents of the windows property and all subviews of all views you can see that the UIAlertView is in a separate window that overlays the main window. Here I have a navbar with a viewcontroller and a tableview (I removed its subviews since they're not relevent).
<UIWindow: 0x411fd50; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x4120af0>>
: <UILayoutContainerView: 0x4123310; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x411f800>>
: | <UINavigationTransitionView: 0x4123500; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x41232e0>>
: | : <UIViewControllerWrapperView: 0x4519d30; frame = (0 64; 320 416); autoresize = W+H; layer = <CALayer: 0x4519a40>>
: | : | <UITableView: 0x7808000; frame = (0 0; 320 416); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x45182a0>>
: | <UINavigationBar: 0x45018b0; frame = (0 20; 320 44); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x4500fe0>>
: | : <UINavigationItemView: 0x4522a20; frame = (100 8; 160 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4526310>>
: | : <UINavigationItemButtonView: 0x45230a0; frame = (5 7; 87 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4520260>>
<_UIAlertOverlayWindow: 0x4179b70; frame = (0 0; 320 480); opaque = NO; layer = <CALayer: 0x4188dc0>>
: <UIAlertView: 0x4194bc0; frame = (3.8 161.95; 312.4 177.1); transform = [1.1, 0, 0, 1.1, 0, 0]; opaque = NO; animations = { transform=<CABasicAnimation: 0x4191160>; opacity=<CABasicAnimation: 0x41226f0>; }; layer = <CALayer: 0x4144c30>>
: | <UILabel: 0x4177e70; frame = (12 15; 260 23); text = 'Name of Date'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4179370>>
: | <UILabel: 0x418b100; frame = (12 45; 260 41); text = 'Name of the date that you...'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4128450>>
: | <UIThreePartButton: 0x41942a0; frame = (11 102; 262 43); opaque = NO; tag = 1; layer = <CALayer: 0x4191f30>>
Here is the code that produces the dump. I found it useful on occasion to see what is going on when something isn't doing what I expect:
void dumpView(UIView* aView, NSString* indent) {
if (aView) {
NSLog(#"%#%#", indent, aView); // dump this view
if (aView.subviews.count > 0) {
NSString* subIndent = [[NSString alloc] initWithFormat:#"%#%#",
indent, ([indent length]/2)%2==0 ? #"| " : #": "];
for (UIView* aSubview in aView.subviews) dumpView( aSubview, subIndent );
[subIndent release];
}
}
}
void dumpWindows() {
for (UIWindow* window in [UIApplication sharedApplication].windows) {
dumpView(window, #"dumpView: ");
}
}
The active UIAlertView lives in a separate window (_UIAlertOverlayWindow). Use .windows property to find it.
The whole UI runs in the main thread.
From the problem that you describe with windows and timing, sounds like you should implement alertView:didDismissWithButtonIndex:. You can trigger your followup code from within that method.
EDIT: if that did not work, I'd try doing a delay to execute the FB stuff after a delay when the window is really guaranteed to be gone.

Removing UITextField from superview does not make it disappear on screen

I have the following code
// Breakpoint here
[label removeFromSuperview];
[label release];
label = nil;
stepping through it with the debugger outputs
(gdb) po [self subviews]
<NSCFArray 0x476af70>(
<UIImageView: 0x47581a0; frame = (0 0; 232 81); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x476b3d0>>,
<UILabel: 0x4758870; frame = (15 11; 202 56); text = 'Test'; clipsToBounds = YES; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x476b590>>
)
(gdb) po label
<UILabel: 0x4758870; frame = (15 11; 202 56); text = 'Test'; clipsToBounds = YES; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x476b590>>
(gdb) n
(gdb) n
(gdb) n
(gdb) po [self subviews]
<NSCFArray 0x478c4e0>(
<UIImageView: 0x47581a0; frame = (0 0; 232 81); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x476b3d0>>
)
Yet it is still visible, it does not disappear. Not even if I do [self setNeedsDisplay] immediately after.
Has anyone else come across this? Is it a bug in the SDK or am I missing something?
It turns out a bug in my code elsewhere was causing multiple identical views to be created on top of each other, leading to this behaviour.