开发者

having trouble adding a view as a subview to multiple view - iphone dev

开发者 https://www.devze.com 2023-02-28 07:18 出处:网络
I need to add a view to multiple views in app. I am running a function which loops through all the UIViews and add that view as subview.

I need to add a view to multiple views in app. I am running a function which loops through all the UIViews and add that view as subview.

I have done something like this:

-(void) setViewForEachSegment: (UIVi开发者_Go百科ew*)view {
    int i;
    for (i = 0; i < [segments count]; i++) {
        [[segments objectAtIndex:i] addSubview: view]
    }
}

here segments is an array of UIViews.

Now, what is happening is that the subview is added only to the last segment.

Has anyone else faced the same problem? I have been working on it for many hours, but don't know how to fix it.


you can't add one view into multiple views, because a view could have single parent view at a time. which is represented by superView property of your UIView.

Each time Create a new object of your UIView before adding to View,


Per the UIView class reference:

A parent view may contain any number of subviews but each subview has only one superview, which is responsible for positioning its subviews appropriately.

So each time you add the view to a different parent view, it is removed from the previous parent view. You will need to change your view hierarchy around to accommodate what you are trying to do, or you will need multiple subviews to add to each parent view.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号