在向其添加子视图后更新自定义uiview的框架

时间:2015-02-14 02:30:50

标签: ios objective-c uiview

我有一个很大的心理障碍,想出一些我认为容易的事情。请看这个非常短的视频:http://screencast.com/t/eaW7rbECv4Ne

我想在layoutSubviews方法中围绕覆盖子视图的整个区域绘制一个矩形。我现在在每个术语周围绘制黄色矩形,如您在视频中看到的那样。

我有一个StatementView。在每个按键点击中,我正在创建新的objectView并将其添加到我的StatementView中,如下所示。我将objectViews添加到containerView并尝试获取containerView的原点和大小,以便在它周围绘制笔划。但是,它总是给我0.

在向子视图添加子视图后,我应该如何更新containerViews边界值?

//  Created by ilteris on 1/31/15.
//  Copyright (c) 2015 ilteris. All rights reserved.
@implementation QWZStatementView
-(id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = NO;
        self.textField = [[MyUITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        self.textField.inputView   = [LNNumberpad defaultLNNumberpad];
        self.textField.delegate = self;
        self.isNumerator = NO;
        self.isDenominator = NO;
        [self addSubview:self.textField];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statementOneTextFieldChanged:) name:UITextFieldTextDidChangeNotification object:nil];
        self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self addSubview:self.containerView];
        self.autoresizesSubviews = YES;
        self.objectsArray = [NSMutableArray arrayWithCapacity:1];
        self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;            
    }
    return self;
}
-(void)statementOneTextFieldChanged:(NSNotification *)notification {   
    NSLog(@"statementOneTextFieldChanged");
    QWZObjectView *objectView = [[QWZObjectView alloc] initWithFrame:self.bounds];
    [self.containerView addSubview:objectView];
    QWZTerm* term = [QWZQuestionDetailViewController createAndReturnCoreDataTermForJSONDict:objectData];
    [objectView createTerm:term];
    [self.objectsArray addObject:objectView];
    [self setNeedsLayout];
}
- (void)layoutSubviews {
    NSLog(@"layoutSubviews StatementView");
    [super layoutSubviews];
    CGSize totalSize  = CGSizeMake(0, 0);
    for (QWZObjectView* objectView in self.objectsArray) {
        CGSize textSize = objectView.bounds.size;
        objectView.frame = CGRectMake(totalSize.width , totalSize.height, textSize.width , textSize.height);
        totalSize.width = textSize.width + totalSize.width;
    }
    CGRect bounds = self.containerView.bounds;
/*
    NSLog(@"self.containerView.bounds is %@", NSStringFromCGRect(self.containerView.bounds)); //always 0. 
    bounds.origin = CGPointMake(totalSize.width/2, self.containerView.bounds.origin.y); //adding this code didn't help at all.
    CGFloat borderWidth = 2.0f;
    self.bounds = CGRectInset(self.bounds, -borderWidth, -borderWidth);
    self.layer.borderColor = [UIColor redColor].CGColor;
    self.layer.borderWidth = borderWidth;
*/
    bounds.origin = CGPointMake(totalSize.width/2, self.containerView.bounds.origin.y);
    self.containerView.bounds = bounds;
}
@end

0 个答案:

没有答案