Strange UIImageView in UIScrollView

2020-07-02 12:05发布

问题:

Very strange behavior, there is a round dot in the center of the screen using this code, and a UIScrollview with nothing inside in a nib. I expect that UIScrollview should be empty. The dot blurs and disappears when I scroll the screen.

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *subviews = [closetScroll subviews];
    UIImageView *strange=[subviews objectAtIndex:0];
    strange.center = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2);
    strange.alpha=1;

    NSLog(@"%@",subviews);
}

The console output is:

<UIImageView: 0x4b1f780; frame = (380.5 508.5; 7 7); opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x4b1f820>

Does anyone know why?

回答1:

After magnifying the UIImageView and tweaking with configurations, I have come to conclude that the UIImageView is actually the scroll bar, and if horizontal and vertical scroll is enabled, an "empty" UIScrollview has two subviews inside.



回答2:

I had the same issue. Calculating the number of subviews can be very deceptive, because of this "feature".

Start the application and count the number of subviews, this will be 1. Now, use you mouse in the simulator or finger on the device and swipe from the right to the left. Count the number of subviews. The number will be 2.

I can deduce nothing else than that this extra UIImageView is produced by Cocoa Touch to render the background in the right color when "bouncing" beyond the end of the UIScrollView's bounds.