开发者

Objective-C - get all subviews of main window's contentView

I want to gather the x/y coordinates and w/h of all subviews of the main window's contentView. In IB I have a view object, vHUD. Eventually I will be adding others either in IB or programmatically. I will have them for various UI purposes sliding in and out of the viewable area. So the thought is that I could write one toggle method, collect all the coordinate data at awakeFromNib, set some rules and then on, 开发者_StackOverflowfor example, button click, pass the view to the a method, find it's properties in a dictionary, (x, y, h, w, toggle rules) and then execute the rule.

So in awakeFromNib I have:

- (void) awakeFromNib { 

     [self getViewData: [mainWin contentView]];
     NSLog(@"%@", subviews);
}

and in getViewData:

- (void ) getViewData : (NSView* ) v { 

     subviews = [[v subviews] mutableCopy];


}

But that didn't seem to work as it returned null to my log. Any idea of how I could do this?


UIView is not conforming to the NSMutableCopying protocol, not even NSCopying actually. So any attempt to call copy or mutableCopy will fail.

In your case I would just not take a copy, or even try to. Work with what is. Do it fast and on the main thread and you can be sure the hierarchy is stable for your calculations.


Not sure to understand your question you get your main window subviews by doing:

[[mainWin contentView] subviews]

Now if you want to COPY these subviews it's a bit trickier, because as PeyloW rightfully mentioned, UIView doesn't respond to the NSMutableCopying protocol.

So you can write a category of UIView and add a method called - (UIView*) forceCopy that will recursively create new UIViews and goes trough its children recursively:

@implementation UIView (YourCategory)

- (UIView*) forceCopy
{
    if (self == nil)
        return nil;

    UIView* aNewView = [[[self class] alloc] initWithFrame:self.frame];

    [aNewView setBackgroundColor:[self backgroundColor]];
    [aNewView setCenter:[self center]];
    [aNewView setContentMode:[self contentMode]];
    [aNewView setHidden:[self isHidden]];
    [aNewView setAlpha:[self alpha]];
    [aNewView setOpaque:[self isOpaque]];
    [aNewView setTag:[self tag]];

    for (UIView* aChildenView in [self subviews])
    {

        UIView* aPotentialNewView = [aChildenView forceCopy];
        if (aPotentialNewView)
            [aNewView addSubview:aPotentialNewView];
    }
    return aNewView;
}

@end

BUT be carefuly you will need to create as many categories as there are different type of views in your main window (UIButton, UILabel ...) and add their properties accordingly for example on UILabel

@implementation UILabel (YourCategory)

- (UIView*) forceCopy
{
    UILabel* aNewLabel = (UILabel*)[super forceCopy];
    if (aNewLabel)
    {
        [aNewLabel setFont:[self font]];
        [aNewLabel setText:[self text]];
        [aNewLabel setTextAlignment:[self textAlignment]];
        [aNewLabel setTextColor:[self textColor]];
        [aNewLabel setLineBreakMode:[self lineBreakMode]];
        [aNewLabel setMinimumFontSize:[self minimumFontSize]];
        [aNewLabel setAdjustsFontSizeToFitWidth:[self adjustsFontSizeToFitWidth]];
    }
    return aNewLabel;
}

@end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜