在iOS中以编程方式适应方面?

在iOS中以编程方式适应方面?,ios,objective-c,cgrect,rect,aspect-fit,Ios,Objective C,Cgrect,Rect,Aspect Fit,我想使用“aspect fit”选项将一个视图插入另一个视图。但出于某些原因,我不能只设置contentMode 您能否根据外部CGRect调整内部CGRect的大小来提供解决方案 是的,有很多类似的问题,但没有人提供这样的解决方案 // Your First View UIView *firstView = self; // Your second View UIView *secondView = [[UIView alloc] initWithFrame:

我想使用“aspect fit”选项将一个视图插入另一个视图。但出于某些原因,我不能只设置
contentMode

您能否根据外部
CGRect
调整内部
CGRect
的大小来提供解决方案

是的,有很多类似的问题,但没有人提供这样的解决方案

    // Your First View
    UIView *firstView = self;

    // Your second View
    UIView *secondView = [[UIView alloc] initWithFrame:CGRectZero]; change
    newSubview.translatesAutoresizingMaskIntoConstraints = NO;
    [containerView addSubview:newSubview];

    // Top Constraint
    [firstView addConstraint:[NSLayoutConstraint constraintWithItem:secondView
                                                              attribute:NSLayoutAttributeTop
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:firstView
                                                              attribute:NSLayoutAttributeTop
                                                             multiplier:1.0
                                                               constant:0.0]];
     // Leading Constraint                                                      
    [firstView addConstraint:[NSLayoutConstraint constraintWithItem:secondView
                                                              attribute:NSLayoutAttributeLeading
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:firstView
                                                              attribute:NSLayoutAttributeLeading
                                                             multiplier:1.0
                                                               constant:0.0]];
    // Bottom Constraint
    [firstView addConstraint:[NSLayoutConstraint constraintWithItem:secondView
                                                              attribute:NSLayoutAttributeBottom
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:firstView
                                                              attribute:NSLayoutAttributeBottom
                                                             multiplier:1.0
                                                               constant:0.0]];
    // Trailing Constraint
    [firstView addConstraint:[NSLayoutConstraint constraintWithItem:secondView
                                                              attribute:NSLayoutAttributeTrailing
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:firstView
                                                              attribute:NSLayoutAttributeTrailing
                                                             multiplier:1.0
                                                               constant:0.0]];

}
我想这对你有帮助

我写了一个函数,可以实现你想要的。 将给定的
innerRect
缩放到适合给定的
outerRect
后,它将返回给定的
cRect
,同时保持纵横比。
innerRect
将在
outerRect
内居中

static inline CGRect aspectFitRect(CGRect outerRect, CGRect innerRect) {

    // the width and height ratios of the rects
    CGFloat wRatio = outerRect.size.width/innerRect.size.width;
    CGFloat hRatio = outerRect.size.height/innerRect.size.height;

    // calculate scaling ratio based on the smallest ratio.
    CGFloat ratio = (wRatio < hRatio)? wRatio:hRatio;

    // The x-offset of the inner rect as it gets centered
    CGFloat xOffset = (outerRect.size.width-(innerRect.size.width*ratio))*0.5;

    // The y-offset of the inner rect as it gets centered
    CGFloat yOffset = (outerRect.size.height-(innerRect.size.height*ratio))*0.5;

    // aspect fitted origin and size
    CGPoint innerRectOrigin = {xOffset+outerRect.origin.x, yOffset+outerRect.origin.y};
    CGSize innerRectSize = {innerRect.size.width*ratio, innerRect.size.height*ratio};

    return (CGRect){innerRectOrigin, innerRectSize};
}
在这种情况下,
innerRect
对于
outerRect
来说太宽,因此它将根据其宽度进行缩放。

在这里,红色区域是
innerRect
,绿色区域是
outerRect
它们原本具有相同的高度,但经过缩小(宽度为2倍)后,
innerRect
现在具有
outerRect
的一半高度

static inline CGRect aspectFitRect(CGRect outerRect, CGRect innerRect) {

    // the width and height ratios of the rects
    CGFloat wRatio = outerRect.size.width/innerRect.size.width;
    CGFloat hRatio = outerRect.size.height/innerRect.size.height;

    // calculate scaling ratio based on the smallest ratio.
    CGFloat ratio = (wRatio < hRatio)? wRatio:hRatio;

    // The x-offset of the inner rect as it gets centered
    CGFloat xOffset = (outerRect.size.width-(innerRect.size.width*ratio))*0.5;

    // The y-offset of the inner rect as it gets centered
    CGFloat yOffset = (outerRect.size.height-(innerRect.size.height*ratio))*0.5;

    // aspect fitted origin and size
    CGPoint innerRectOrigin = {xOffset+outerRect.origin.x, yOffset+outerRect.origin.y};
    CGSize innerRectSize = {innerRect.size.width*ratio, innerRect.size.height*ratio};

    return (CGRect){innerRectOrigin, innerRectSize};
}

此处,
innerRect
作为子视图添加到
outerRect
如果要将它们添加到同一superview中,可以将
outerRect
传递到函数中,而不是
边界

以下是CGSize和CGRect扩展,分别用于在另一个大小内拟合大小和在另一个矩形内拟合矩形:

extension CGSize
{
    func sizeThatFitsSize(_ aSize: CGSize) -> CGSize
    {
        let width = min(self.width * aSize.height / self.height, aSize.width)
        return CGSize(width: width, height: self.height * width / self.width)
    }
}

extension CGRect
{
    func rectThatFitsRect(_ aRect:CGRect) -> CGRect
    {
        let sizeThatFits = self.size.sizeThatFitsSize(aRect.size)

        let xPos = (aRect.size.width - sizeThatFits.width) / 2
        let yPos = (aRect.size.height - sizeThatFits.height) / 2

        let ret = CGRect(x: xPos, y: yPos, width: sizeThatFits.width, height: sizeThatFits.height)
        return ret
    }
}

如果你正在使用CGRect,它不会自动调整所有大小的屏幕,这是一个很好的解决方案。是的,但我需要完全通过CGRect,正如我所写的。我已经处理了所有调整大小的事件您可以在其他视图中创建枚举或字符串,并在viewdidLoad中设置它。这意味着在转到另一个控制器时,使用AspectFit设置tat枚举,并在
viewdidLoad中设置tat枚举