Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView - Moving view to front, but below scroll indicators

I am working with a custom view which inherits from UIScrollView. The problem I am having is adding a UILabel that should be displayed at the 'top', in front of the other views. It works, but unfortunately it also covers up the scroll indicators.

enter image description here

As you can see, the scroll indicator is being obscured. I don't want to disable scroll indicators.

I am adding the views directly to the UIScrollView:

UILabel *subsection = [[UILabel alloc] initWithFrame:CGRectMake(0, y * h, 
    [self totalContentWidth], [tableViewDelegate rowHeight])];
[subsection setText:subsectionName];
[subsection setTextAlignment:NSTextAlignmentCenter];

[subSectionRows setValue:subsection forKey:key];

[self addSubview:subsection];

and then bringing them to the front, which

- (void) bringSubsectionsToFront {   
    for (UIView* row in [self.subSectionRows allValues]) {
        [self bringSubviewToFront:row];
    }
}

This behavior is confusing to me. If I peek at UIView.layer.zPosition of all the UIScrollView's subviews, they are all the same.

If I adjust the bringSubsectionsToFront method to instead move the labels in front of the view that contains the grid lines, the behavior is the same.

Looking at some internal view classes whose behavior works, it looks as if they are being added to the scroll view with: [self insertSubview:cell atIndex:0];

What am I missing here?

Solution: verec remarked that I could find the bars if I really wanted to. So I iterated over the subview list at a suitable point, and sure enough, there they were. The order of the objects was as follows:

CustomCell
...
UIImageView
UIImageView
CustomBorderView
UILabel
UILabel

Assuming this is the z ordering, no wonder my labels were always on top.

Solution becomes simple; add UILabel's after CustomCells. This solution only works because the custom table adds CustomCells at index 0, which I believe makes scroll indicators appear after the CustomCells. If this assumption holds, I can reorder the other views relative to the CustomCells, achieving the layering effect I want.

like image 929
Danedo Avatar asked Oct 04 '22 15:10

Danedo


1 Answers

The simplest is to use and intermediate UIView, call it 'contentView' whose children are the views you want to bringToFront.

If that contentView is what is inside the scrollView, it will stay 'below' the scroll indicators whichever contentView child you bring to the front.

The alternative would be to scan into the scrollView looking for the scroll indicators (you will find them!) but that's a compatibility risk, as you never know if/when Apple may decide to remove/reshape/reposition them according to some new (flatter ...) design paradigm ...

like image 175
verec Avatar answered Oct 12 '22 18:10

verec