Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将UILabel的边界与同一子视图中的姊妹UIView成比例对齐_Ios_Storyboard_Autolayout_Alignment - Fatal编程技术网

Ios 将UILabel的边界与同一子视图中的姊妹UIView成比例对齐

Ios 将UILabel的边界与同一子视图中的姊妹UIView成比例对齐,ios,storyboard,autolayout,alignment,Ios,Storyboard,Autolayout,Alignment,我有一个静态图像,上面有一个区域,用来显示一些多行文字。就像视频游戏中角色的讲话泡泡。我抓到一张股票图片,看起来像这样: 我将UIImageView设置为在主视图的子视图中进行方面匹配(参见此)。我在子视图内部也设置了一个UILabel,它将保存多行文本。我希望能够在屏幕上移动子视图,使其具有任意大小,并且仍然使UIImageView保持相同的外观,并使UILabel适合图像的气泡 我做了一个测试,它已经设置好了 我打算通过设置与UIImageView的中心x和y成比例的约束,将UILabel

我有一个静态图像,上面有一个区域,用来显示一些多行文字。就像视频游戏中角色的讲话泡泡。我抓到一张股票图片,看起来像这样:

我将UIImageView设置为在主视图的子视图中进行方面匹配(参见此)。我在子视图内部也设置了一个UILabel,它将保存多行文本。我希望能够在屏幕上移动子视图,使其具有任意大小,并且仍然使UIImageView保持相同的外观,并使UILabel适合图像的气泡

我做了一个测试,它已经设置好了

我打算通过设置与UIImageView的中心x和y成比例的约束,将UILabel的边界保持在语音气泡区域内。对于我的图像,左边缘乘数为0.65,右边缘乘数为1.8,顶部为0.19,底部为0.63

我编写了几个从UIView扩展的函数来确认这一点:

/**
Draws a vertical line proportional to the center x of the view.

A `proportional` value of 0 is the left edge, while 2 is the right edge.

:param: proportion The value from the left edge (0.0) to the right edge (2.0)
:param: inColor The color to draw the line in (red by default)
*/
func drawVertLineAtProportion(proportion: CGFloat, inColor: UIColor = UIColor.redColor()) {
  let size = self.frame.size
  
  UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
  let context = UIGraphicsGetCurrentContext()
  
  let x = self.bounds.origin.x + proportion*self.frame.size.width/2
  
  var vertPath = UIBezierPath()
  vertPath.lineWidth = size.height/150.0
  vertPath.moveToPoint(CGPointMake(x, 0))
  vertPath.addLineToPoint(CGPointMake(x, self.frame.size.height))
  inColor.setStroke()
  vertPath.stroke()
  
  let image = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  
  self.addSubview(UIImageView(image: image))
}
DrawhorizLineat比例类似,但适用于水平线

我可以通过在
viewdide
中运行这4行来确认乘数值是否正确:

imageView.drawVertLineAtProportion(0.65)
imageView.drawVertLineAtProportion(1.8)
imageView.drawHorizLineAtProportion(0.19)
imageView.drawHorizLineAtProportion(0.63)
然后imageView如下所示:

我可以将包含imageView的子视图的大小更改为我想要的任何大小,并且imageView保持纵横比合适,并且这些红线相交处的红色框始终正是我想要的

那么,当我设置UILabel的边的约束以遵循相同的公式时,为什么边不对齐

似乎当imageView的宽度达到最大值时,左右边缘是正确的,但顶部和底部是错误的:

如果高度最大,则顶部和底部是正确的,但左侧和右侧是错误的:

那么为什么UILabel边界没有与红线对齐呢

编辑以指定我知道存在运行时修复,但我想知道为什么故事板不工作。 如果我在
viewdide
中添加这些行以修复UILabel的框架,它将起作用:

let upperLeft: CGPoint = CGPointMake(imageView.frame.origin.x + 0.65*imageView.frame.size.width/2, imageView.frame.origin.y + 0.19*imageView.frame.size.height/2)
let lowerRight: CGPoint = CGPointMake(imageView.frame.origin.x + 1.8*imageView.frame.size.width/2, imageView.frame.origin.y + 0.63*imageView.frame.size.height/2)
let size: CGSize = CGSizeMake(lowerRight.x - upperLeft.x, lowerRight.y - upperLeft.y)

speechLabel.frame = CGRectMake(upperLeft.x, upperLeft.y, size.width, size.height)

但是我仍然想知道为什么我在故事板中设置的东西不起作用。

我找到了一个解决方案,完全在故事板中,这让我得到了我想要的,尽管我确实相信我最初的问题表明Xcode中有一个bug

我注意到,由于Xcode似乎不尊重我想要将
speechLabel
与姐妹UI元素
imageView
对齐,我只是将我正在对齐的UIView设置为不再是
imageView

我实现的解决方案是将
imageView
speechLabel
放入一个新的父视图,我称之为
aspectFitView
。我在
imageView
上设置的所有约束都是为了使其符合方面,我改为使用
aspectFitView

然后我设置了
imageView
的约束,使其成为
aspectFitView
的大小:将中心x和y对齐,并使宽度和高度相等

既然
speechLabel
的父项是
imageView
的确切大小、位置和纵横比,我为
speechLabel
设置了与
aspectFitView
相反的比例约束,瞧。一切都很好。它在我的故事板上看起来是正确的,我不需要添加任何后ViewDidDisplay帧校正代码,而且它在我运行它的任何设备上都能完美工作

我为感兴趣的人更新了解决方案