Author Topic: Setting Anchors Programmatically on ScrollView Children  (Read 9633 times)

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Setting Anchors Programmatically on ScrollView Children
« on: March 21, 2014, 01:16:20 PM »
I have a UISprite (that is also a UIButton) that I am dynamically instantiating as a child of a scroll view / UI grid. I've set the anchor for the scroll view to be the width of it's container so it expands horizontally. The UISprite/Button is a prefab, which can't store it's anchor until it knows what it's parent is.

      mySprite.leftAnchor.target = transform.parent;
      mySprite.leftAnchor.absolute = 17;

      mySprite.rightAnchor.target = transform.parent;
      mySprite.rightAnchor.absolute = -17;

      mySprite.ResetAnchors();

is the code I'm using to try and anchor the UISprite/Button to it's parent (the scrollview). However, for some reason I look at the scene after that code has run and the anchors for all those sprite-buttons is None.

Any idea what I'm doing wrong?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting Anchors Programmatically on ScrollView Children
« Reply #1 on: March 21, 2014, 01:17:41 PM »
Why are you anchoring something to a scroll view? Scroll view's contents move, but if something is anchored, how can it move?

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Setting Anchors Programmatically on ScrollView Children
« Reply #2 on: March 21, 2014, 02:35:49 PM »
It's a vertically scrolling scroll view, and I want the buttons to expand to the width of it when switching between iPhone and iPhone. Shouldn't they be able to anchor to the left and right safely?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting Anchors Programmatically on ScrollView Children
« Reply #3 on: March 21, 2014, 03:23:58 PM »
If it scrolls vertically then yes you can anchor left and right.

You're missing a call to UpdateAnchors(); by the way. Look at UIRect.SetAnchor.

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Setting Anchors Programmatically on ScrollView Children
« Reply #4 on: March 21, 2014, 03:27:08 PM »
I did! I saw you call update and reset. Calling both of them or one or the other doesn't help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting Anchors Programmatically on ScrollView Children
« Reply #5 on: March 21, 2014, 04:12:09 PM »
So what's the code exactly?

The order should be:
1. Instantiate the prefab.
2. Set its anchors.

If you do 2 before 1, it obviously won't work.

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Setting Anchors Programmatically on ScrollView Children
« Reply #6 on: March 21, 2014, 04:16:04 PM »
So I have a class that generates all the items in the scroll view:

  1. public void generateContentItems()
  2.         {
  3.                 for( int i = 0; i < testItemCount; i++ )
  4.                 {
  5.                         NGUITools.AddChild(ContentScrollView.gameObject, LevelItem);
  6.                         ContentGrid.Reposition();
  7.                 }
  8.         }
  9.  

and in each LevelItem's awake function goes this stuff:

  1. void Awake()
  2.         {
  3.                 // Set Colors
  4.                 myButton.defaultColor = VL_UIColors.Instance.NormalContentButton;
  5.                 myButton.pressed = VL_UIColors.Instance.TappedContentButton;   
  6.                 myButton.hover = VL_UIColors.Instance.NormalContentButton;
  7.  
  8.                 // Set Anchors
  9.                 mySprite.leftAnchor.target = transform.parent;
  10.                 mySprite.leftAnchor.absolute = 17;
  11.  
  12.                 mySprite.rightAnchor.target = transform.parent;
  13.                 mySprite.rightAnchor.absolute = -17;
  14.  
  15.                 mySprite.ResetAnchors();
  16.                 mySprite.UpdateAnchors();
  17.         }
  18.  

The scroll view object is anchored to it's parent's parent, and also has a UIGrid script on it set to vertically arrange things.

That's pretty much it!

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Setting Anchors Programmatically on ScrollView Children
« Reply #7 on: March 21, 2014, 04:49:16 PM »
I should also point out that dragging my button prefab into the scene and manually setting it's left and right anchors to the scroll view works fine when switching between iPad and iPhone... for some reason it's just doing it on the fly that's breaking it..

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting Anchors Programmatically on ScrollView Children
« Reply #8 on: March 21, 2014, 05:09:35 PM »
Awake() is not the right place for what you're doing. Awake() is for setting local variables. It's not the right place to call functions.

Awake() is executed as soon as the component is added. This happens before the object's parent is set.

Change it to be Start() instead, and in the future never put anything in Awake() that calls external functionality.

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Setting Anchors Programmatically on ScrollView Children
« Reply #9 on: March 21, 2014, 05:38:17 PM »
Yup, I came back to this thread to say that I just tried that and it fixed it. You're right, i'm not sure why I didn't think of that.