How can I get the index of sub UIView in the parent view?

后端 未结 4 1730
闹比i
闹比i 2021-01-05 02:11

I want to use exchangeSubviewAtIndex: withSubviewAtIndex: method.

but how can i get the indexes of those views? i already have their pointers, so I

4条回答
  •  佛祖请我去吃肉
    2021-01-05 02:57

    You can find out the index of your object as you find in NSArray, because view.subviews return an array

    NSUInteger index1=[parentView.subviews indexOfSubview:subview1];
    NSUInteger index2=[parentView.subviews indexOfSubview:subview2];
    
    [parentView exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];
    

    You can follow this link for more How to get UIView hierarchy index ??? (i.e. the depth in between the other subviews)

提交回复
热议问题