I've got a Scrollview that whenever I update it from a script, some of the time (not always), the grid contents are offset by a couple pixels. Basically, I have code to update the anchors differently based on if you're holding the device portrait or landscape.
My hierarchy looks like this:
>Main Navigation Panel
--> OtherContent
--> SomethingElseContent
--> Scrollview Master UIWidget (I resize this widget to resize everything)
---->Background Image (inset by a few pixels so if you drag on the edges of the scrollview, it'll still scroll)
----> UIPanel (Anchored to the Master Widget) / UIScrollView / SpringPanel
------>UIGrid
-------> square content1
-------> square content2
-------> square content3, etc
My code looks like this when I rotate the device:
void EnterViewLandscape(){
panelGrid.leftAnchor.Set(0.0f, -9.0f); //this is the panel anchored to the master widget
panelGrid.rightAnchor.Set(1.0f, 9.0f);
panelGrid.bottomAnchor.Set(0.0f, 12.0f); //this adjusts the clipping size at runtime to be appropriate to the orientation of the device
panelGrid.topAnchor.Set(1.0f, -12.0f);
panelGrid.UpdateAnchors();
backgroundImage.leftAnchor.Set(0, 9); //this is the background image, inset by a few pixels
backgroundImage.rightAnchor.Set(1, -9);
backgroundImage.bottomAnchor.Set(0, 0);
backgroundImage.topAnchor.Set(1, 0);
backgroundImage.UpdateAnchors();
foreach(UIWidget widget in gridWidgets)
{ //this is the UIGrid contents
BoxCollider collider = (BoxCollider)widget.collider;
collider
.size = new Vector3
( 160,
115,
0); widget.SetDimensions(160, 115);
widget.UpdateAnchors();
}
panelGrid.RebuildAllDrawCalls();
panelGrid.Refresh();
// i have no idea if any of this is is necessary or in the right order...
scrollView.RestrictWithinBounds(true);
scrollView.ResetPosition();
grid.repositionNow = true;
grid.Reposition();
}