Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
在Swift中使用patternImage时图像未居中_Swift_Uiimage_Uilabel - Fatal编程技术网

在Swift中使用patternImage时图像未居中

在Swift中使用patternImage时图像未居中,swift,uiimage,uilabel,Swift,Uiimage,Uilabel,在我的应用程序中,我使用patternImage在UILabel上显示背景图像,如下所示: self.boxBackground = UIImage(named: "emptyBox") self.backgroundColor = UIColor(patternImage: boxBackground) UILabel是一个自定义类,因此它是在init方法中设置的,而boxBackground是我正在使用的UIImage。图像是自定义UILabel(40x46)的精确尺寸 直到最近Xcode

在我的应用程序中,我使用patternImage在UILabel上显示背景图像,如下所示:

self.boxBackground = UIImage(named: "emptyBox")
self.backgroundColor = UIColor(patternImage: boxBackground)
UILabel是一个自定义类,因此它是在init方法中设置的,而boxBackground是我正在使用的UIImage。图像是自定义UILabel(40x46)的精确尺寸

直到最近Xcode更新(Swift 5)时,图像才居中显示。现在,它稍微向上和向左平铺,因此图像看起来像是已沿对角线向左移动了5个点。奇怪的是,如果UILabel中有文本,它就会正确显示,一旦删除文本,它就会返回到不居中/平铺的外观


我是做错了还是这是一个bug?

您能解释一下为什么要使用图案图像颜色吗?这里的目标是什么?@matt嗨,matt,这是游戏的一部分,我展示了一个卡通盒子的图像作为UILabel的背景。根据标签中的内容,卡通框会改变颜色(但尺寸保持不变)。屏幕上同时有多个这样的标签。这并不能解释为什么你需要在故事中使用图案颜色。一个盒子和一个标签对我来说并不意味着图案颜色。@matt不确定你在找什么?我只是想找到一种显示图像的方法,而不是为UILabel的背景设置颜色。patternImage函数运行良好,满足了我的需求,直到更新后它的行为开始有所不同。如果有更好的方法来显示图像,那么我很高兴听到这个消息,我在谷歌上搜索了不少,每篇文章都指向patternImage。谢谢,我不知道文章是怎么说的。从iOS 3.2开始,我就一直在编写iOS程序,在那段时间里,我从未使用过图案图像颜色。如果我这样做,我会在标签后面显示图像。