【问题标题】:Resize UIView not working调整 UIView 大小不起作用
【发布时间】:2012-04-28 15:29:12
【问题描述】:

我有一个从视图控制器调用的 uiview 子类。它显示了一切,但我无法调整它的大小。当我在视图的 initwithframe 方法上设置断点时,它显示不变,但视图控制器肯定在改变它...

这是我的视图控制器代码:

- (void)resizeWithTag:(int)tag wasTouched:(BOOL)touched

    {
        GameBox *tbox = (GameBox *)[self.view viewWithTag:tag];
        CGRect frame = tbox.frame;
        frame = CGRectMake(0, 0, 300, 300);
        tbox.frame = frame;
        [tbox setNeedsDisplay];
        NSLog(@"%f", tbox.frame.size.width);
    }

这里是uiview子类的init方法和draw方法

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1]];
        name = [[UILabel alloc] init];
        self.autoresizesSubviews = YES;
    }
    return self;
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    int border = 3;
    UIImage *image = [UIImage imageNamed:@"Sunflower.jpeg"];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1);
    CGContextStrokeRectWithWidth(context, CGRectMake(0, 0, rect.size.width, rect.size.height), border);
    [self attachImage:image inRect:rect inBorder:border];
    [name setFrame:CGRectMake(0 + border/2, rect.size.height-10-border/2, rect.size.width-(border), 10)];
    [name setFont:[UIFont fontWithName:@"Helvetica" size:9.0]];
    [name setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.0]];

    name.textAlignment = UITextAlignmentCenter;
    [self addSubview:name];
}

【问题讨论】:

    标签: iphone uiview uiviewcontroller


    【解决方案1】:

    initWithFrame: 中放置断点不是寻找的正确位置。创建视图时只会调用一次。

    稍后您将通过调用tbox.frame = someFrame 来获取视图并更改其框架属性,这相当于调用setFrame:

    这一行

    NSLog(@"%f", tbox.frame.size.width);
    

    应该表明你确实改变了宽度。

    【讨论】:

    • 是的。但是屏幕上的宽度不会改变
    猜你喜欢
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多