【问题标题】:Resize subviews of a UIView调整 UIView 的子视图大小
【发布时间】:2014-09-02 21:21:35
【问题描述】:

我正在尝试在子视图中调整子视图 (UIImageView) 的大小,但我可以处理它。

我有一个 UIView,其中包含五个 UIImageViews 作为子视图,并排:UIView 的宽度为 400 像素,每个子视图的宽度为 80 像素(它们的 xOrigins 是 0、80、160、... )。

如何将 UIVIew 调整为 800 px 宽度并自动将其子视图调整为 160 px 宽度和 xOrigins 在 0、160、320、...?

UIViewAutoresizingFlexibleHeight、UIViewAutoresizingFlexibleWidth、UIViewAutoresizingFlexibleLeftMargin、UIViewAutoresizingFlexibleRightMargin、UIViewAutoresizingFlexibleTopMargin、UIViewAutoresizingFlexibleBottomMargin 的不同组合并没有解决我的问题。

有什么帮助吗?

代码: UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 400, 200)]; mainView.autoresizesSubviews = YES; [self.view addSubview:mainView];

UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 200)];
view1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
view1.backgroundColor = [UIColor color1];
[mainView addSubview:view1];

UIImageView *view2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 80, 80, 200)];
view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
view2.backgroundColor = [UIColor color2];
[mainView addSubview:view2];

UIImageView *view3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 160, 80, 200)];
view3.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
view3.backgroundColor = [UIColor color3];
[mainView addSubview:view3];

UIImageView *view4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 240, 80, 200)];
view4.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
view4.backgroundColor = [UIColor color4];
[mainView addSubview:view4];

UIImageView *view5 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 320, 80, 200)];
view5.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
view5.backgroundColor = [UIColor color5];
[mainView addSubview:view5];

【问题讨论】:

  • 只尝试这两个: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth
  • 启用自动布局会更容易实现
  • 可以在没有情节提要的情况下使用自动布局吗?
  • 当然。如果没有(通常默认启用)或以编程方式创建约束,只需在 .xib 中启用它。只是不要忘记自动布局需要部署到 iOS 6.0 或更高版本。

标签: ios objective-c uiview resize


【解决方案1】:

当您使用 CGRectMake 时,您使用的是固定宽度,您应该像这样使这些值彼此相对:

@interface ViewController ()
{
    UIView *mainView;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 200)];
    mainView.backgroundColor = [UIColor grayColor];

    UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 200)];
    view1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    view1.backgroundColor = [UIColor redColor];
    [mainView addSubview:view1];

    UIImageView *view2 = [[UIImageView alloc] initWithFrame:CGRectMake(view1.frame.origin.x + view1.frame.size.width, 0, view1.frame.size.width, view1.frame.size.height)];
    view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    view2.backgroundColor = [UIColor greenColor];
    [mainView addSubview:view2];

    UIImageView *view3 = [[UIImageView alloc] initWithFrame:CGRectMake(view2.frame.origin.x + view2.frame.origin.y, 0, view1.frame.size.width, view1.frame.size.height)];
    view3.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    view3.backgroundColor = [UIColor blueColor];
    [mainView addSubview:view3];

    UIImageView *view4 = [[UIImageView alloc] initWithFrame:CGRectMake(view3.frame.origin.x + view3.frame.size.width, 0, view1.frame.size.width, view1.frame.size.height)];
    view4.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    view4.backgroundColor = [UIColor yellowColor];
    [mainView addSubview:view4];

    UIImageView *view5 = [[UIImageView alloc] initWithFrame:CGRectMake(view4.frame.origin.x + view4.frame.size.width, 0, view1.frame.size.width, view1.frame.size.height)];
    view5.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    view5.backgroundColor = [UIColor purpleColor];
    [mainView addSubview:view5];

    [self.view addSubview:mainView];


    // update width of main view to 800 pixel after 2 seconds
    [self performSelector:@selector(updateWidth) withObject:nil afterDelay:2.0];
}

-(void)updateWidth
{
    CGRect newFrame = mainView.frame;

    newFrame.size.width = 800;

    mainView.frame = newFrame;

    NSLog(@"View updated");
}

这样,视图是相对于彼此的,而不是静态值。这是我使用上面的代码得到的结果:

之前

之后

自动布局方法

如果您想知道,如果您想走这条路,我还为您提供了一种自动布局方法:

@interface ViewController ()
{
    UIView *mainView;
    NSLayoutConstraint *mainViewWidthConstraint;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self initView];

    // update width of main view to 800 pixel after 2 seconds
    [self performSelector:@selector(updateWidth) withObject:nil afterDelay:2.0];
}

-(void)initView
{
    mainView = [[UIView alloc] init];
    mainView.backgroundColor = [UIColor blackColor];
    mainView.clipsToBounds = YES;
    mainView.translatesAutoresizingMaskIntoConstraints = NO;

    UIImageView *view1 = [[UIImageView alloc] init];
    view1.backgroundColor = [UIColor redColor];
    view1.translatesAutoresizingMaskIntoConstraints = NO;


    UIImageView *view2 = [[UIImageView alloc] init];
    view2.backgroundColor = [UIColor blueColor];
    view2.translatesAutoresizingMaskIntoConstraints = NO;


    UIImageView *view3 = [[UIImageView alloc] init];
    view3.backgroundColor = [UIColor yellowColor];
    view3.translatesAutoresizingMaskIntoConstraints = NO;


    UIImageView *view4 = [[UIImageView alloc] init];
    view4.backgroundColor = [UIColor purpleColor];
    view4.translatesAutoresizingMaskIntoConstraints = NO;


    UIImageView *view5 = [[UIImageView alloc] init];
    view5.backgroundColor = [UIColor grayColor];
    view5.translatesAutoresizingMaskIntoConstraints = NO;


    [mainView addSubview:view1];
    [mainView addSubview:view2];
    [mainView addSubview:view3];
    [mainView addSubview:view4];
    [mainView addSubview:view5];

    [self.view addSubview:mainView];

    id views = @{
                 @"mainView": mainView,
                 @"view1": view1,
                 @"view2": view2,
                 @"view3": view3,
                 @"view4": view4,
                 @"view5": view5
                 };

    mainViewWidthConstraint = [NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:400];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainView]" options:0 metrics:nil views:views]];

    // main view constraint
    [self.view addConstraint:mainViewWidthConstraint];

    // subviews constraints
    [mainView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:mainView attribute:NSLayoutAttributeWidth multiplier:1.0/5.0 constant:0.0]];

    [mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view1][view2(==view1)][view3(==view1)][view4(==view1)][view5(==view1)]|" options:0 metrics:nil views:views]];

    [mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1(200)]|" options:0 metrics:nil views:views]];
    [mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view2(==view1)]|" options:0 metrics:nil views:views]];
    [mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view3(==view1)]|" options:0 metrics:nil views:views]];
    [mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view4(==view1)]|" options:0 metrics:nil views:views]];
    [mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view5(==view1)]|" options:0 metrics:nil views:views]];
}

-(void)updateWidth
{
    mainViewWidthConstraint.constant = 800;

    [self.view layoutIfNeeded];

    NSLog(@"View updated");
}

同样的结果。

【讨论】:

    猜你喜欢
    • 2013-04-12
    • 2012-08-28
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多