如果我们在真正的预提交处理程序中,由于 CA 限制,我们实际上不能添加任何新的栅栏——这可能意味着什么吗?
问题描述
我试图找出为什么自定义 UIButtons 集合不起作用.在
每次旋转时,以下消息也会在 Xcode 的调试区域中出现两次.
消息四次出现三次,即当手机倒置时没有消息.当我在我的以前的帖子 或代码如下所示.在每种情况下,选中或取消选中上下颠倒框都没有区别.
我也试过
这里是代码
不用担心栅栏警告信息.它似乎是无害的,不是由您正在做的任何事情引起的.
您发布的代码有几个问题:
您创建
myView
并限制其中心,但您不给它任何大小限制.此外,myView
是一个局部变量,您不向myView
添加任何子视图.所以myView
是一个不可见的、无大小的视图,没有内容.你为什么要创建它?您正在使用裸形状图层绘制uberCircle".裸"是指没有视图的
layer
属性是该层.裸层不参与自动布局.您根据顶层视图边界的中心计算每个按钮的位置.您在
viewDidLoad
期间执行此操作,但在调整顶级视图大小以适应当前设备之前调用了viewDidLoad
.因此,在某些设备上,您的方向盘甚至不会在启动时居中.您没有对按钮设置任何约束,也没有设置它们的自动调整大小掩码.结果是,当设备旋转时,顶层视图会调整大小,但每个按钮的位置(相对于顶层视图的左上角)保持不变.
启用倒置"复选框不足以在 iPhone 上允许倒置方向,仅在 iPad 上.
以下是您需要进行的更改:
使用视图绘制uberCircle".如果要使用形状层,请创建
UIView
的子类,该子类使用CAShapeLayer
作为其层.您可以从 this answer 复制ShapeView
类.设置从 uberCircle 中心到顶层视图中心的约束,以在顶层视图改变大小时保持 uberCircle 居中.
对于每个按钮,设置从按钮中心到顶层视图中心的约束,以在顶层视图改变大小时保持按钮正确定位.这些约束需要非零常量来使按钮偏离中心.
覆盖
supportedInterfaceOrientations
以启用倒置方向(除了选中倒置"复选框之外).去掉
viewDidLoad
中的myView
.你不需要它.去掉
centre
属性.你不需要它.
因此:
I am trying to find why a collection of custom UIButtons does not work. In a version described in my earlier post I created a circle of UIButtons programmatically in Swift 3 and anchored the circle to the centre of the screen. That version used a subclass of UIView - based on an Apple Swift tutorial (Implement the Button Action) - together with an implementation of autolayout that draws on Imanou Petit’s excellent code examples (No.6 Anchor). In that version I managed to get my buttons to rotate successfully when the iPhone rotates but the button action-target fails to work.
So I have now tried an alternative version using a viewcontroller instead of a subclass of UIView. This time the same button action-target works but rotating the phone causes the image to shift away from the centre as shown below.
With each rotation the following message also appears twice in the debug area of Xcode.
The message happens three times out of four, i.e. there is no message when the phone is turned upside down. This occurs when I run either the code in my previous post or the code shown below. And in each case it made no difference whether the Upside Down box was checked or un-checked.
I also tried disabling OS_ACTIVITY MODE but that changed nothing except hide a message that might potentially explain the problem. Someone more experienced than me will hopefully recognise what this debug message means either in the context of my previous code (shown here) or my latest code, shown below.
ORIGINAL CODE
I am still in the process of learning Swift so it doesn’t matter at this stage whether the solution uses a viewcontroller or a subclass of UIView so long as I can arrange a circle of UIButtons that still work after I configure them using autolayout. Every suggestion is welcome. Thanks.
SOLUTION
The message that appeared in Xcode’s debug area - and which I used in the subject line of this post - was clearly not the issue. Thanks to Rob Mayoff, NSLayoutConstraint now computes the dimensions and position of each button whereas these were computed prior to run-time in my original code. His solution along with several other improvements are now reflected in the code below. To this I added the original action-target for the buttons. These not only work but remain locked to the centre of the view whenever the device orientation changes.
The code can easily be made to work for a different size configuration by changing values for radius, buttonCount and buttonSideLength (see table).
Here is the code
Don't worry about the fences warning message. It seems to be harmless and not caused by anything you're doing.
There are several problems with the code you posted:
You create
myView
and constrain its center, but you don't give it any size constraints. Furthermore,myView
is a local variable and you don't add any subviews tomyView
. SomyView
is an invisible, sizeless view with no contents. Why are you creating it at all?You're drawing your "uberCircle" using a bare shape layer. By "bare", I mean there's no view whose
layer
property is that layer. Bare layers don't participate in autolayout.You compute the position of each button based on the center of the top-level view's bounds. You're doing this during
viewDidLoad
, butviewDidLoad
is called before the top-level view has been resized to fit the current device. So your wheel won't even be centered at launch on some devices.You don't set any constraints on the buttons, or set their autoresizing masks. The result is that, when the device rotates, the top-level view resizes but each button's position (relative to the top-left corner of the top-level view) stays the same.
Turning on the "Upside Down" checkbox is not sufficient to allow upside-down orientation on iPhones, only on iPads.
Here are the changes you need to make:
Use a view to draw the "uberCircle". If you want to use a shape layer, make a subclass of
UIView
that uses aCAShapeLayer
for its layer. You can copy theShapeView
class from this answer.Set constraints from the center of the uberCircle to the center of the top-level view, to keep the uberCircle centered when the top-level view changes size.
For each button, set constraints from the center of the button to the center of the top-level view, to keep the button positioned properly when the top-level view changes size. These constraints need non-zero constants to offset the buttons from the center.
Override
supportedInterfaceOrientations
to enable upside-down orientation (in addition to checking the "Upside Down" checkbox).Get rid of
myView
inviewDidLoad
. You don't need it.Get rid of the
centre
property. You don't need it.
Thus:
这篇关于如果我们在真正的预提交处理程序中,由于 CA 限制,我们实际上不能添加任何新的栅栏——这可能意味着什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!