Objective-C autolayout Questions

Asked 2 years ago, Updated 2 years ago, 135 views

I tried to set ImageView as a child of ScrollView on the Objective-C storyboard and autolayout as ImageView, but it didn't apply properly no matter how much I tried, and the ImageView became strange (I want to set up space on top, bottom, left, right).What should I do in this case?

objective-c xcode scrollview uiimage

2022-09-30 20:16

1 Answers

Does "On the Objective-C storyboard" mean that you want to set up Auto Layout in the program (NSLayoutConstraint)?Or do you want to use the mouse on the Storyboard?

Anyway, I'd like to keep this as an overview of Auto Layout.

The basic idea of Auto Layout is to impose constraints that uniquely determine the location and size of objects.There is no better limit than a good limit, but the point is that constraints that can be multiple possibilities result in errors.
The most frequently used constraints in practice are shown below.

Enter a description of the image here

Determining the horizontal (X-coordinate) and vertical (Y-coordinate) spacing of nearby objects and determining the width and height of the object uniquely determines the location and size of the object.

However, if you look at the objects in the scroll view, the location of the objects is not uniquely determined.This is because the content size in the scroll view is indefinite.If you want the program to place objects in a scroll view without Auto Layout, you must first place the CGSize value in the UIScrollView property contentSize and then place the Subview.
When Auto Layout places objects in a scroll view, it is not uniquely determined until you determine the location and size of the objects, as well as the content size.Specifically, the constraints are as shown in the picture below.

Enter a description of the image here

From the previous picture, there are two additional restrictions.Only by setting constraints on all edges of the content area in the scroll view, plus the width and height of the plus object, and a total of six constraints can a unique position and size be determined.At the same time, content space size has been determined.


2022-09-30 20:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.