【发布时间】:2013-10-07 11:41:19
【问题描述】:
我有两个UIViews,比如containerView 和testView。假设testView 的宽度大于containerView 的宽度。在这种情况下,当我将testView 作为子视图添加到containerView 时,我的问题就出现了。 testView 的框架超出了containerView 的范围。这是我的代码-
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(60, 154, 200, 200)];
containerView.backgroundColor = [UIColor grayColor];
containerView.clipsToBounds = NO;
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 100)];
testView.backgroundColor = [UIColor yellowColor];
testView.layer.borderWidth = 3;
testView.layer.borderColor = [[UIColor blackColor] CGColor];
testView.center = CGPointMake(containerView.frame.size.width / 2, containerView.frame.size.height / 2);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 230, 50)];
label.text = @"This is a Label to test the size";
[testView addSubview:label];
[containerView addSubview:testView];
[self.view addSubview:containerView];
}
此代码的输出是 -
containerView.clipsToBounds = YES; 时,输出为 -
但我真正需要的是—— (已编辑图片)
当我添加一个宽度/高度大于其父视图-containerView 的testView 时,应调整testView(包括子视图)的框架,使其完全适合其父视图。
我欢迎你的想法..!
【问题讨论】:
-
你每次手动设置testView的大小??从哪里来?
-
我只是运行你指定的相同代码,我没有给出任何问题,我的意思是输出是你需要的。
-
@Bala:我在 Xcode 5.0、iOS7 中运行。它与版本有关吗?
-
我在 xcode 4.2 中使用 iOS6 运行代码,我没有遇到任何问题..
标签: iphone ios objective-c uiview ios7