6

I have a UIView with a UIImageView at the very top, a UILabel below it, a UIButton below that, and a UISegmentedControl that determines what determines what embedded UIview to display at the bottom (which also a choice to not show any at all).

I've run into the problem where I've set up all of my constraints in the interface builder, and everything seems to be fine when I switch between screen sizes in the storyboard. However when I actually run the project on a device or emulated, the UIimage at the top is briefly stretched before "snapping" into a size the fits the constraints. Also, it seems as if the label disappears for a brief second and reappears after the image has snapped into a size. After the "snap" has occurred, everything is in place and there are no problems.

This snapping occurs both when testing on a 4 and 3.5 inch display. I find this odd because I've designed the UI for the 4inch screen perfectly.

Does anyone know why this is happening?

Edit Here's whats the constraints look like in IB.

Constraints

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
p0ny
  • 329
  • 1
  • 3
  • 11
  • Do you have any warnings in Xcode about your constraints? It could be that the frames in the storyboard do not match the constraints. – Andrew Sula Aug 10 '14 at 18:58
  • No Warning about any constraints. I used their suggested constraints and everything looks great in the interface builder – p0ny Aug 10 '14 at 19:03
  • Could you post a screenshot of your constraints in IB? – Acey Aug 10 '14 at 19:18
  • Out of interest, which version of Xcode are you using? – Andrew Sula Aug 12 '14 at 07:32
  • We had a similar issue, which was caused by some `margin` values within the storyboard. check here http://stackoverflow.com/questions/26510725/uipageviewcontroller-child-controller-ignoring-status-bar-height-during-scrolli – longi Dec 23 '14 at 08:41

3 Answers3

6

This is potentially due to adjustments that you might be making to your UI elements (or constraints) from the view controller in code. For example, if you are programmatically setting a different UIImage into your UIImageView, and this code is happening too late in the view lifecycle (for example, in viewDidAppear) after a layout pass has already calculated view positions and sizes, then you will see a visible snap as views take on new positions based on the new intrinsic content size of the image view.

This could be caused by other adjustments such as injecting a localized string into a UILabel in code, which causes the label to have a smaller or larger intrinsic content size, which in turn affects the layout based on your constraints.

If you are making adjustments to your UI in code, make sure they are happening in viewDidLoad or viewWillAppear: so that they occur before the view's initial layout pass (and the view's animation onscreen).

If you're still seeing issues, you can try explicitly forcing an immediate layout pass to occur on the view controller's view at the end of viewWillAppear: by doing the following:

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

    [self.view setNeedsLayout];
    [self.view layoutIfNeeded]; // forces an immediate layout pass
}
smileyborg
  • 30,197
  • 11
  • 60
  • 73
2

Spacing between the UIElements might be the problem . Specially when using Pickers

coreDeviOS
  • 1,468
  • 2
  • 14
  • 27
2

Got it, You have applied constraint for Picker to adjust along with segmented control. So when app runs on 4 inch screen , picker automatically fits properly, but for 3.5 Inch picker always starts from bottom of the screen and picker will push segmented control upwards and hence segmented control will push rest control automatically. Remove picker and segmented control constraint.

Bizcharts
  • 44
  • 2