gpt4 book ai didi

ios - 展开有关在自动布局中更改方向和屏幕尺寸的 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:15:45 24 4
gpt4 key购买 nike

我有一个 UIImageView,我需要在更改方向和屏幕尺寸时扩展(高度和宽度)。我为此使用了自动布局约束。

    topImageView.contentMode = UIViewContentModeScaleAspectFit;
topImageView.backgroundColor = [UIColor clearColor];
topImageView.layer.cornerRadius = 5.0f;
topImageView.clipsToBounds = YES;
topImageView.translatesAutoresizingMaskIntoConstraints = NO;

if(login_DO.logoPath)
[topImageView loadImage:login_DO.logoPath];

[self.view addSubview:topImageView];

NSArray *horizontalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-(%i)-[topImageView(%f)]",X_OFFSET,VIEW_FRAME_WIDTH-X_OFFSET*2]
options:0 metrics:nil views:@{@"topImageView": topImageView}];

NSArray *verticalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-(%f)-[topImageView(80)]",navHeight]
options:0 metrics:nil views:@{@"topImageView": topImageView}];

[self.view addConstraints:horizontalConstraints];
[self.view addConstraints:verticalConstraints];

NSLayoutConstraint *leadingMarginForImageConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeLeadingMargin
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view attribute:
NSLayoutAttributeLeadingMargin multiplier:1.0 constant:X_OFFSET];

NSLayoutConstraint *topMarginForImageConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeTopMargin
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view attribute:
NSLayoutAttributeTopMargin multiplier:1.0 constant:VIEW_FRAME_WIDTH-X_OFFSET*2];

[self.view addConstraints:@[ leadingMarginForImageConstraint,
topMarginForImageConstraint]];

但是图片没有展开。我是自动布局的新手。我是否缺少任何约束?

最佳答案

您可以将 imageBottomConstraint 从 -navHeight 更改为从底部开始的其他值。避免使用 VIEW_FRAME_WIDTH,因为它会在您改变方向时改变。

UIView *superview = self.view;
NSLayoutConstraint *imageTopConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:superview
attribute:NSLayoutAttributeTop multiplier:1.0 constant:navHeight];
NSLayoutConstraint *imageBottomConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual toItem:superview
attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-navHeight];
NSLayoutConstraint *imageLeftConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual toItem:superview attribute:
NSLayoutAttributeLeft multiplier:1.0 constant:X_OFFSET];
NSLayoutConstraint *imageRightConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual toItem:superview attribute:
NSLayoutAttributeRight multiplier:1.0 constant:-X_OFFSET];
[superview addConstraints:@[imageBottomConstraint ,
imageLeftConstraint, imageRightConstraint,
imageTopConstraint]];

更多帮助请查看http://www.tutorialspoint.com/ios/ios_auto_layouts.htm

或尝试使用 https://github.com/marcoarment/CompactConstraint

如果有帮助,请告诉我。

关于ios - 展开有关在自动布局中更改方向和屏幕尺寸的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26135018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com