开发者

objective c addSubview order

i am a junior programmer I face a problem of some of the subview in the program not shown , don't know if it is the addsubview order problem or the others . hope if anyone can provide a suggestion or solution .thanks all

for (int i = 0; i < NUM_DISHES; i++) {            

     UIImageView* img_steps = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]]];
        //IMPORTANT: The i*W_IPAD means that 1st 0*1024 , 1*1024 , 2*1024.............and so on
        [img_steps setFrame:CGRectMake(W_IPAD, 0, W_IPAD , H_UPFR)];


        UIImageView* imageset = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[ary_img objectAtIndex:i]]];
        [imageset setFrame:CGRectMake(89, 9, H_INGREPHOTO, W_INGREPHOTO)];
        [ary_image addObject:imageset];


        //ingredient amount section
        UILabel* lbl_amt = [[UILabel alloc] initWithFrame:CGRectMake(128, 177, 190, 33)];
        [lbl_amt setText:[ary_amount objectAtIndex:i]];
        [lbl_amt setFont:[UIFont systemFontOfSize:25]];
        [lbl_amt setTextColor:[UIColor blackColor]];
        [lbl_amt setBackgroundColor:[UIColor clearColor]];
        [ary_amt addObject:lbl_amt];


    //method section
        UILabel开发者_StackOverflow社区* lbl_method = [[UILabel alloc] initWithFrame:CGRectMake(596, 93, 130, 32)];
        [lbl_method setText:[ary_meth objectAtIndex:i]];
        [lbl_method setBackgroundColor:[UIColor clearColor]];
        [lbl_method setFont:[UIFont systemFontOfSize:30]];
        [lbl_method setTextColor:[UIColor blackColor]];
        [ary_method addObject:lbl_method];


    //alpha bar step number step number section
        UILabel* lbl_numberstep = [[UILabel alloc] initWithFrame:CGRectMake(90 + (130 * i), 5, W_NUMBERSTEP, H_NUMBERSTEP)];
        [lbl_numberstep setText:[NSString stringWithFormat:@"%d",i + 1]];
        [lbl_numberstep setFont:[UIFont systemFontOfSize:20]];
        [lbl_numberstep setBackgroundColor:[UIColor clearColor]];
        [lbl_numberstep setTextColor:[UIColor blackColor]];



        [self.view addSubview:img_steps];
        [alpha_bar addSubview:lbl_numberstep];   
        [alphaframe addSubview:[ary_method objectAtIndex:i]];
        [alphaframe addSubview:[ary_amt objectAtIndex:i]];
        [alphaframe addSubview:[ary_image objectAtIndex:i]];
    }

    // Section --- Add Subview

    [self.view addSubview:background];

    [background addSubview:main_view];


    [main_view addSubview:alpha_bar];
    [main_view addSubview:alphaframe];

    [main_view addSubview:but_transit_to_a];   //can add to the main_view       
    [main_view addSubview:left_buttom];
    [main_view addSubview:right_buttom];

    [alpha_bar addSubview:bar];
    [alpha_bar addSubview:lbl_lastnum];
    [alphaframe addSubview:but_transit_to_c];

    }


    return self;
}


To bring the subview to front you can use [yourMainView bringSubviewToFront:yoursubview]. If your subview isn't showing then, check that you've properly set up the subview's frame and that hidden = NO.


The view.subviews array holds the bottom-most view in index zero, and holds views in an order relating to their "z-index". So [myView.subviews lastObject]; will always give the view in front of all other views and [myView.subviews objectAtIndex:0]; will have the view below all views in that array.

From the UIView docs - subviews property:

You can use this property to retrieve the subviews associated with your custom view hierarchies. The order of the subviews in the array reflects their visible order on the screen, with the view at index 0 being the backmost view. For complex views declared in UIKit and other system frameworks, any subviews of the view are generally considered private and subject to change at any time. Therefore, you should not attempt to retrieve or modify subviews for these types of system-supplied views. If you do, your code may break during a future system update.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜