【发布时间】:2013-02-26 20:20:48
【问题描述】:
我在课堂上创建了一个自定义 UIView。然后我将它添加到我的屏幕上,我想知道它是CGRect。但从表面上看,我似乎无法获得这个特定UIView 的大小。这是我的代码:
CGRect labelFrame;
if (!tickerImage) {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 8, upperTextField.frame.origin.y, 0, 0);
} else {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 16, upperTextField.frame.origin.y, 0, 0);
}
SGNewLabel = [[SGAdressLabel alloc] initWithFrame:labelFrame];
[labelsArray addObject:SGNewLabel];
[self.view addSubview:SGNewLabel];
[SGNewLabel addNewLabelWithText:labelText andTickerImage:tickerImage];
UIView *lastLabel = [labelsArray lastObject];
CGRect newTextFieldFrame = CGRectMake(lastLabel.frame.origin.x + lastLabel.frame.size.width, labelFrame.origin.y, 320, 30);
upperTextField.frame = newTextFieldFrame;
如您所见,我根据我的自定义 SGNewLabel 的框架设置了 upperTextField 框架,这似乎是错误的,即使放入 NSArray 也是如此。我在labelFrame 声明的末尾设置了0, 0,因为我不知道未来对象的大小,这就是我的问题的根源。
如何更正它并获得正确的 UIView 大小?
编辑: NSLog 在我看来给了我这个:
NSLog(@"The rect of object is: %@", NSStringFromCGRect(self.frame));
2013-02-11 17:16:02.333 MyApp[2383:c07] The rect of object is: {{24, 55}, {0, 0}}
UIView 本身是正常绘制的,我可以看到它的全尺寸。
【问题讨论】:
-
你试过
yourView.frame吗? -
@rptwsthi 这就是我在
lastLabel.frame中所做的,它给了我与lastLabel或SGNewLabel相同的结果 -
如日志所说,您需要自定义视图的框架
-
@rptwsthi 但是如果我不知道它的长度或高度,我该怎么做呢?它应该是灵活的
-
在获取其子视图的累积尺寸后设置它。当子视图的所有值都已设置时。
标签: iphone ios objective-c cocoa-touch uiview