简体   繁体   English

对象框架在运行时会缩放,但会保留恒定值,从而以编程方式难以/不准确地放置项目

[英]Object frames scale at runtime, but retain constant values, making placing items programmatically difficult/inaccurate

I'm building an iPhone app for devices running iOS 8 and above, so the app has to be compatible with all iPhones since the 4S. 我正在为运行iOS 8及更高版本的设备构建iPhone应用程序,因此该应用程序必须与4S以来的所有iPhone兼容。

First, in the storyboard file, I created a view that is the same size as the 4S screen. 首先,在情节提要文件中,创建了一个与4S屏幕大小相同的视图。 On that view, I added a grid of 3 rows of 4 rectangles each. 在该视图上,我添加了3行网格,每行4个矩形。 I added Autolayout constraints, and everything is playing nicely for the time being. 我添加了“自动布局”约束,目前一切运行良好。 There are no errors or warnings related to misplaced views or conflicting constraints, etc. The rectangles are scaling nicely on all the iPhone simulators. 没有与错误放置的视图或冲突的约束等相关的错误或警告。在所有iPhone模拟器上,矩形都可以很好地缩放。

My problem comes when I try to create and place a button programmatically. 当我尝试以编程方式创建和放置按钮时,就会出现我的问题。 The squares in my grid are placeholders where these buttons need to be placed. 我的网格中的正方形是需要放置这些按钮的占位符。 So, when I create a new button and set it's frame, I'm setting it equal to the frame of one of the rectangles on my grid. 因此,当我创建一个新按钮并将其设置为框架时,会将其设置为等于网格上一个矩形的框架。 When I run the app on the 4S simulator, it works fine, but on every other simulator (with a larger screen), the button is not appearing in the proper place. 当我在4S模拟器上运行该应用程序时,它运行良好,但在其他所有模拟器(屏幕较大)上,该按钮均未出现在正确的位置。

What appears to be happening is that each of the rectangles has a set X and Y value in the storyboard editor, and when the app runs in any of the simulators, the X and Y values are remaining constant, even though the view itself is scaling. 似乎正在发生的情况是,每个故事矩形在情节提要编辑器中都有一个设置的X和Y值,并且当应用程序在任何模拟器中运行时,即使视图本身正在缩放,X和Y值也保持不变。 。

To put it more clearly, let's say that slot #2 has an x value of 150 and ay value of 200. Whenever I try to place the button in slot #2 by setting the button's frame equal to slot #2's frame and then run the app, the button's X and Y values are ALWAYS being set to 150 and 200 respectively, instead of being scaled to compensate for the change in size of the device screen. 为了更清楚地说,假设插槽2的x值为150,y值为200。每当我尝试通过将按钮的框架设置为等于插槽2的框架,将按钮放置在插槽2中时,再运行在应用程序中,按钮的X和Y值始终分别设置为150和200,而不是按比例缩放以补偿设备屏幕尺寸的变化。 Slot #2 itself is scaling properly, but the button set to Scale #2's frame is being placed at 150/200 regardless. 插槽2本身可以正确缩放,但是无论如何,设置为缩放2帧的按钮都放置在150/200位置。

Has anybody ever experienced this, and if so, do you know how I would get the button's frame to scale with the other elements on the screen? 有没有人经历过这种情况,如果是这样,您是否知道我将如何使按钮的框架与屏幕上的其他元素成比例?

Let me know if I haven't been clear. 让我知道是否不清楚。 Perhaps I can post some pictures to illustrate what I'm getting at. 也许我可以张贴一些图片来说明我的意思。

Edit: Adding some images: 编辑:添加一些图像:

Running on the 4S Simulator. 在4S模拟器上运行。 The card appears where it's supposed to in the grid 该卡将显示在网格中的预期位置 4S Simulator上的图像。该卡将显示在网格中的预期位置

This is on the 6+ Simulator. 这是在6+模拟器上。 As you can see, the card appears basically exactly where it would if I were running it on the 4S simulator. 如您所见,该卡的显示基本上与我在4S模拟器上运行该卡时的位置完全一样。 When I created it, though, I set it's frame equal to the frame of the second square on the grid. 但是,当我创建它时,我将其框架设置为等于网格上第二个正方形的框架。

在6+模拟器上。如您所见,该卡的显示基本上与我在4S模拟器上运行该卡时的位置完全一样。但是,当我创建它时,我将其框架设置为等于网格上第二个正方形的框架。

You need to use auto layout to position / size things, so that they will have the same relative arrangement no matter how big the screen is. 您需要使用自动布局来定位/调整大小,以便无论屏幕有多大,它们都将具有相同的相对排列。 "Set the frame" is the opposite of auto layout. “设置框架” 自动布局相反 Don't set any frames; 不要设置任何框架; use auto layout throughout. 始终使用自动布局。

Alternatively you can use frames, but then timing becomes crucial. 另外,您可以使用帧,但是定时就变得至关重要。 You need to wait until after viewDidLayoutSubviews , because only then does the grid have the size it will actually have when it appears. 您需要等到viewDidLayoutSubviews之后,因为只有这样,网格才具有实际出现时的大小。 If you do set a card's frame too early (eg in viewDidLoad ), you will be using the nib grid size, not the runtime grid size. 如果确实过早设置了卡的框架(例如,在viewDidLoad ),则将使用笔尖网格大小,而不是运行时网格大小。

You should not set the frame instead of it use the auto layout constraint . 您不应设置框架,而应使用auto layout constraint Create the place card button and addsubview it on any Rectangles button/view then add constraints programmatically. 创建地方卡按钮和addsubview它放在任何矩形按钮/视图然后以编程方式添加约束。 Try the KVConstraintExtensionsMaster library. 尝试使用KVConstraintExtensionsMaster库。

By Using KVConstraintExtensionsMaster library, put the below code from where you want to place new card upon Ractangle Buttons either 

   UIButton *placeCardButton = [UIButton prepareNewViewForAutoLayout];
   [placeCardButton setBackgroundColor:[UIColor redColor]];
   [self.secondRactangleButton prepareViewForAutoLayout];
   // here self.secondRactangleButton is IBOutlet reference.
   [self.secondRactangleButton addSubview:placeCardButton];

   // now adding all required constraints on the button
   [placeCardButton applyConstraintFitToSuperview];

Or Direct Without using library 

    UIButton * placeCardButton = [UIButton new];
    [placeCardButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [placeCardButton setBackgroundColor:[UIColor redColor]];
    [self.secondRactangleButton addSubview:placeCardButton];

    [self.secondRactangleButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[placeCardButton]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(placeCardButton)]];
    [self.secondRactangleButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[placeCardButton]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(placeCardButton)]];

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

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