1
votes

I'm loading a view from a storyboard programatically, but there's an issue with my subview heights (I'm using Autoresize Subviews etc).

When the view loads, the main UIView appears to be the correct size, but the subviews that depend on the constraints to dynamically calculate their final height don't seem to be resizing from the sizes they were set at in interface builder.

Loading the view like so:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ApplicationRecordingViewController* recordingController = (ApplicationRecordingViewController*)[storyboard instantiateViewControllerWithIdentifier:@"recordView2"];
[self presentViewController:recordingController animated:NO completion:nil];

Interface Builder

Orange area's height is from top of main view to top of grey area. Grey area is fixed height

The strange thing is, if I set the simulated size in interface builder as the correct view for my phone, the sizes work out. I guess the initial heights for subviews are 'baked' into the view, but then (usually) resized when the view actually loads?

Note: The orange area should be completely filled by the camera view I'm loading into it.

incorrect height

Incorrect height

correct height

Correct height if I change simulated layout

Would love to know what I'm doing wrong!

Edit 1: Constraints

Camera is the orange bit. The Camera bottom constraint is currently set as the bottom of the view + enough room for the gray bar. Have also tried setting the bottom to match the top of the gray bar!

Constraints

Constraints 2

Link to Storyboard with Scene

5
What constraints have you set for the various views?Paulw11
I've added the constraints as an edit!Paul
You aren't positioning the overlay view horizontally except in relation to the blue bar. If you want the overlay to be positioned relative to the bottom of the camera view I would expect to see a constraint to say soPaulw11
Overlay is the red bar. It's positioned directly above of the Gray bar (with fixed height), which it does appear to do when I simulate the correct size of screen for my device.Paul
So which views are incorrect? I think you are relying too much on fixed heights. You typically want at least one object whose height can "float" to take up the remaining space. I would expect to see more top/bottom constraints between the various views as you move down the screenPaulw11

5 Answers

0
votes

I know you're all better than me, but I experienced the same in my last project. My Camera View inside a UIView is not in full screen or resizing and fitting the whole UIView, and I also posted it on my daily blog.

Implement viewDidLayoutSubviews -- Inside that method, layout again your video preview layer.Do not alloc init anything inside that method, just re-layout your views. After that, your video preview will fill the height and width of its parent, or whatever you've been targetting.

The issue also has something to do with the hierarchy of the constraints and views.

enter image description here

0
votes

You already got the answer.

'if I set the simulated size in interface builder as the correct view for my phone, the sizes work out.'

You may call some functions to get height in viewDidLoad or viewWillAppear.

Change Code like this

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];

    CGFloat height = [self getHeight];
}

or

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    CGFloat height = [self getHeight];
}
0
votes

As i can see in your storyboard constrain you have set bottom space to bottom layout guide 150 . i think this is the issue . if you give fixed height to bottom view then no need to set constant as from bottom layout guide --> 150 .

Your old constrain :- >

enter image description here

Just set bottom space of orangeview from greenview = 0.

Check new constrain ,

enter image description here

Here is your storyboard link ,

Storyboard Download link

Here is your output,

enter image description here

0
votes

After investigating the layout a little more, I found the camera view (orange) seemed to be the correct size, but the live camera preview wasn't.

Solution was to move the camera library (SCRecorder)'s init method into viewDidAppear (was in viewDidLoad before).

My thinking is iOS doesn't autolayout the camera preview view in the same way as normal UIViews, etc.

@interface ApplicationRecordingViewController () {
    SCRecorder *_recorder;
}
@end

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    _recorder.previewView = _previewView;
}
-1
votes

Perhaps you can try to get rid of these two hook.

enter image description here