質問

So, I'm trying to make a calendar from an image.

Images are in higher resolution then the screen, so I thought about loading the image to a larger view, adding everything to it (as subviews), resizing the view to fit the screen to show the user, then if he decides to save the image, resizing it again to a larger size and saving it. (It's mostly working as I intended).

The problem I'm having is the subviews I add to my ImageView won't resize.

Loading the image to my ImageView and enabling AutoresizesSubviews:

[self.backgroundImageView setImage:backgroundImage_];
[self.backgroundView setAutoresizesSubviews:YES];

Adding a UILabel on it:

UILabel *calendarLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenWidth_ * 0.1, 0, screenWidth_ * 0.8, screenHeight_ * 0.15)];
[calendarLabel setText:@"Calendar"];
[calendarLabel setTextColor:[UIColor whiteColor]];
[calendarLabel setTextAlignment:NSTextAlignmentCenter];
[calendarLabel setBackgroundColor:[UIColor clearColor]];
[self.backgroundImageView addSubview:calendarLabel];

Saving the size it should be resized to:

savedRect_ = self.backgroundView.frame;

Resizing the imageview (actually resizing the parent of the imageview, since imageView occupies the whole view):

[self.backgroundView setFrame:savedRect_];

Now I have no idea why this isn't working as I intended it.

PS: Did some testing and it turns out it's a little different with the programmatically added views then the ones added with IB. I have a feeling it has to do with AutoresizingMask.

Did some research, and I would imagine I have to make everything flexible (it can grow in width, height, size on top, bottom, right and left sides), but it's still isn't working. If I enable all the 6, the view just doesn't move.

Could anyone tell me what setting I need to make so the views stay at the same position related to their parent View?

役に立ちましたか?

解決

Ok, turned out to be an easy solution as I expected...

I tried applying each mask separately:

[calendarLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[calendarLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
....

Instead of:

[calendarLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top