gpt4 book ai didi

ios - 调整 uiview 的背景图像取决于文本

转载 作者:可可西里 更新时间:2023-11-01 02:20:03 25 4
gpt4 key购买 nike

我想创建可调整大小的用户界面 View 背景。它将根据文本调整大小。我尝试了以下代码:

let capInsetsIncoming = UIEdgeInsets(top: 17, left: 26.5, bottom: 17.5, right: 21)
self.contentView.backgroundColor = UIColor(patternImage: UIImage(named:"profileBioBackground")!.resizableImageWithCapInsets(capInsetsIncoming))

结果:

enter image description here

预期:

enter image description here

这是我的背景图片:

enter image description here

我该如何解决这个问题?

最佳答案

我认为最简单的方法是使用UIImageView作为背景。而且,您必须拆分原始图像,以便可以将其用作可调整大小的图像。

无论如何,这是代码:

class ViewController: UIViewController {

@IBOutlet var contentView: UIView!

override func viewDidLoad() {
super.viewDidLoad()


contentView.backgroundColor = nil

let leftBackgroundView = UIImageView()
let rightBackgroundView = UIImageView()

let image = UIImage(named: "profileBioBackground")!
let scale = image.scale
let size = image.size
let leftRect = CGRect(
x: 0,
y: 0,
width: size.width / 2 * scale,
height: size.height * scale
)
let rightRect = CGRect(
x: size.width / 2 * scale,
y: 0,
width: size.width / 2 * scale,
height: size.height * scale
)

leftBackgroundView.image = UIImage(
CGImage: CGImageCreateWithImageInRect(image.CGImage, leftRect),
scale: scale,
orientation: .Up
)?.resizableImageWithCapInsets(UIEdgeInsets(top: 20, left: 4, bottom: 4, right: 16))
rightBackgroundView.image = UIImage(
CGImage: CGImageCreateWithImageInRect(image.CGImage, rightRect),
scale: scale,
orientation: .Up
)?.resizableImageWithCapInsets(UIEdgeInsets(top: 20, left: 16, bottom: 4, right: 4))

leftBackgroundView.setTranslatesAutoresizingMaskIntoConstraints(false)
rightBackgroundView.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.insertSubview(leftBackgroundView, atIndex: 0)
contentView.insertSubview(rightBackgroundView, atIndex: 0)
let views = ["left": leftBackgroundView, "right": rightBackgroundView]
contentView.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat("H:|[left][right(==left)]|", options: nil, metrics: nil, views: views)
+ NSLayoutConstraint.constraintsWithVisualFormat("V:|[left]|", options: nil, metrics: nil, views: views)
+ NSLayoutConstraint.constraintsWithVisualFormat("V:|[right]|", options: nil, metrics: nil, views: views)
)
}
}

Storyboard设置和结果:

screenshot

关于ios - 调整 uiview 的背景图像取决于文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31340726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com