简体   繁体   English

如何确保UIView缩小到其超级视图的大小?

[英]How do you ensure that a UIView shrinks down to the size of its superview?

I'm creating a custom view (a subclass of UIView), the purpose of which is to hold together a static background image, a static border/frame overlay, and several smaller images that can be rotated, stretched, and dragged around the view (before the whole view is exported as an image). 我正在创建一个自定义视图(UIView的子类),其目的是将静态背景图像,静态边框/框架覆盖图以及几个可以在视图周围旋转,拉伸和拖动的较小图像组合在一起(在将整个视图导出为图像之前)。

I'm stuck on displaying this custom view from within another view. 我被困在从另一个视图中显示此自定义视图。 The images are coming from the iPhone camera, which has a very high resolution, and the only things I have been able to manage so far are cutting off the majority of the image or having my view go over the end of the screen. 这些图像来自具有高分辨率的iPhone相机,到目前为止,我唯一能做的就是截取大部分图像或将我的视线移到屏幕的末端。 I need to be able to display it alongside menu items and so on, without editing the image (so that it can be sent on in its full form). 我需要能够在菜单项等旁边显示它,而无需编辑图像(以便可以完整格式发送图像)。

I am also adding the image to a fullscreen view, and it still will not change size. 我还将图像添加到全屏视图,并且它仍然不会改变大小。

I am initialising the view with as follows 我正在如下初始化视图

- (id)initWithImage:(UIImage *)image
{
    self = [super init];
    if (self) {
        [self setClipsToBounds:YES];
        [self setAutoresizesSubviews:YES];
        [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

        _image = image;
        _imageView = [[UIImageView alloc] initWithImage:_image];
        [_imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
        [self addSubview:_imageView];
    }
    return self;
}

And I add it to another view with 我将其添加到另一个视图

self.card = [[SGImageCard alloc] initWithImage:[SGCardDataController cardImage]];
[self.cardPlaceHolder setAutoresizesSubviews:YES];
[self.cardPlaceHolder setContentMode:UIViewContentModeScaleAspectFit];
[self.cardPlaceHolder addSubview:self.card];

That view has also been set up in Interface Builder, and set there to autoresize its subviews and to cliptobounds (sometimes -- I have tried both ways). 该视图也已在Interface Builder中设置,并在那里设置为自动调整其子视图的大小和Cliptobounds(有时,我已经尝试过两种方法)。 I have added my view in viewdidload (when the placeholder does not have its frame, for a reason I don't know), and also later, when it does (and it doesn't work either way). 我已经在viewdidload中添加了我的视图(当占位符没有框架,由于我不知道的原因),后来又添加了它(无论哪种方式都不起作用)。

What am I missing? 我想念什么? (Have I even gone about this the right way?) (我是否已经以正确的方式解决了这个问题?)

I finally solved this by setting the AutoResizeMasks to 我终于通过将AutoResizeMasks设置为

UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleRightMargin | UIViewAutoResizingFlexibleBottomMargin

And also setting the ContentModes to 并将ContentModes设置为

UIViewContentModeScaleAspectFit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM