Reputation: 367
Okay so I have a subclassed UIScrollView with some subviews inside it, when the device orientation changes I want to update the position of the subviews inside so that they fit the new orientation.
When I just updated the positions of the subviews and then went to scroll I noticed that the scroll handle was ginormous... My loop that went through all of the subviews and updated their frames also set the frame of the scroll handle...
So to get around this.. whenever I added subviews I set a tag on them. When the orientation changed I had a condition around my set frame which checked if the tag on the view matched the subviews I added. If yes then set the frame...
int i = 0;
for (UIView *gridViewItem in self.subviews) {
if (gridViewItem.tag == GRID_CELL) {
gridViewItem.frame = [self calculateCellPosition:i];
}
i++;
}
But now I want to detect a touch on the subviews, and the easiest way I can think of doing that is by using the tag property so I know which subview is being tapped... However the tag is currently being used to help me distinguish the scroll handle from the other subviews...
When i logged out the subviews of my UIScrollView I got this:
2011-05-08 16:07:56.266 XXXXXX[2096:207] (
"<UIView: 0x5a0d3f0; frame = (16 16; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a09250>>",
"<UIView: 0x5a19d70; frame = (167 16; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a17660>>",
"<UIView: 0x5a1d4f0; frame = (16 137; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a1d520>>",
"<UIView: 0x5a1e300; frame = (167 137; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a1e330>>",
"<UIView: 0x5a1e4f0; frame = (16 258; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a1e520>>",
"<UIView: 0x5a1f320; frame = (167 258; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a1f350>>",
"<UIView: 0x5a20120; frame = (16 379; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a20150>>",
"<UIView: 0x5a20fb0; frame = (167 379; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a20fe0>>",
"<UIView: 0x5a21dd0; frame = (16 500; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a21e00>>",
"<UIView: 0x5a22bb0; frame = (167 500; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a22be0>>",
"<UIView: 0x5a239a0; frame = (16 621; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a239d0>>",
"<UIView: 0x5a247b0; frame = (167 621; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a247e0>>",
"<UIView: 0x5a20f30; frame = (16 742; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a20f80>>",
"<UIView: 0x5a26460; frame = (167 742; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a26490>>",
"<UIView: 0x5a27220; frame = (16 863; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a27250>>",
"<UIView: 0x5a20dd0; frame = (167 863; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a28080>>",
"<UIView: 0x5a28e60; frame = (16 984; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a28e90>>",
"<UIView: 0x5a29c50; frame = (167 984; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a29c80>>",
"<UIView: 0x5a2aa60; frame = (16 1105; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2aa90>>",
"<UIView: 0x5a2b870; frame = (167 1105; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2b8a0>>",
"<UIView: 0x5a255a0; frame = (16 1226; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a255f0>>",
"<UIView: 0x5a262f0; frame = (167 1226; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2d610>>",
"<UIView: 0x5a2e3c0; frame = (16 1347; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2e3f0>>",
"<UIView: 0x5a2f1d0; frame = (167 1347; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2f200>>",
"<UIView: 0x5a2ffc0; frame = (16 1468; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2fff0>>"
)
All of the subviews have my tag on it! I can't see the scroll handle? Does anyone know a way of identifying the scroll handle without using a tag like I have?
Your help will be much appreciated! Thanks!
Upvotes: 0
Views: 716
Reputation: 578
I probably wouldn't sub-class UIScrollView for this. Add a single UIView to UIScrollView and add your cells to that view. Then always work with that view. No need for tags and doesn't interfere with UIScrollViews inner workings. Scrolling should work as long as the size of your content view reflects the cells inside it.
Upvotes: 1