【问题标题】:Resize a UIView调整 UIView 的大小
【发布时间】:2013-10-07 11:41:19
【问题描述】:

我有两个UIViews,比如containerViewtestView。假设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; 时,输出为 -

但我真正需要的是—— (已编辑图片)

当我添加一个宽度/高度大于其父视图-containerViewtestView 时,应调整testView(包括子视图)的框架,使其完全适合其父视图。

我欢迎你的想法..!

【问题讨论】:

  • 你每次手动设置testView的大小??从哪里来?
  • 我只是运行你指定的相同代码,我没有给出任何问题,我的意思是输出是你需要的。
  • @Bala:我在 Xcode 5.0、iOS7 中运行。它与版本有关吗?
  • 我在 xcode 4.2 中使用 iOS6 运行代码,我没有遇到任何问题..

标签: iphone ios objective-c uiview ios7


【解决方案1】:

执行此操作的现代方法是创建视图,向视图添加约束以建立与包含视图的关系,然后将该视图添加为子视图。

这样您就不会弄乱框架,并且会为您处理旋转调整大小。

编辑添加

这是一些示例代码,可以执行与您要求的类似的操作

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create the containing view
    UIView *greyView = [[UIView alloc] initWithFrame:CGRectZero];
    greyView.backgroundColor = [UIColor grayColor];
    greyView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:greyView];

    // Create the label to go into the containing view
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.translatesAutoresizingMaskIntoConstraints = NO;
    label.text = @"Example Text";

    label.backgroundColor = [UIColor yellowColor];

    [greyView addSubview:label];

    NSDictionary *bindings = NSDictionaryOfVariableBindings(greyView, label);

    // Set the constraints for the greyView relative to it's superview - to cover completely
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[greyView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[greyView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];

    // Set the constraints for the label to be pinned equally with padding
    [greyView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(30)-[label]-(30)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];
    [greyView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(30)-[label]-(30)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];

}

如果您想看到它的实际效果并自己调整约束,我已经上传了一个 simple example project 供您使用。

【讨论】:

  • 嗨,阿比泽姆。似乎我必须实现它,只使用约束。我没有使用约束的经验。我确实尝试了一些公式但失败了。如何为这种需求创建公式?
  • 一天我尝试了很多。我的结果每次都是白屏。添加约束后containerView和testView的frame变为零。
  • 您确定正确添加了约束吗?您需要将容器视图的约束添加到超级视图以及标签的约束。这里有一个提示 - 不要使用 containerView 作为名称 - 它在使用 Xcode 和 Storyboard 时具有特定含义。
  • 我不确定我使用的是正确的,因为我是使用 Autolayouts 的新手。
  • @iOS 我添加了一个示例和一个可以下载的项目的链接。
【解决方案2】:

为什么将 testView(黄色)的宽度设置为大于其容器的宽度,然后抱怨它变大了?你应该这样做

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(60, 154, 200, 200)];
[...]
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 100)]; // 180 not 260
[...]
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 150, 50)]; // 150 not 230

或者使用建议的自动布局。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多