简体   繁体   English

如何使用Swift以编程方式调整UIImageView边距

[英]How to adjust UIImageView margin programmatically with Swift

I have a UIImageView as part of a UICollectionViewCell that I would like to use to visually display a quantity (like a progress bar). 我有一个UIImageView作为UICollectionViewCell的一部分,我想用它来可视地显示数量(例如进度条)。 In this case the ImageView and its offset represent the percentage remaining of a given item represented by the cell. 在这种情况下,ImageView及其偏移量表示该单元格表示的给定项目的剩余百分比。

Imagine an image of water inside a glass. 想象一下玻璃杯中水的图像。 When the glass is full you see all of the water texture. 当杯子装满时,您会看到所有的水纹理。 When the glass is half full, you see half of the water image. 当杯子装满一半时,您会看到一半的水图像。 so on and so forth... 等等等等...

I want to adjust the top margin of the ImageView based on the percentage remaining. 我想根据剩余百分比调整ImageView的上边距。 for example: 例如:

var image : UIImageView
var item = inventoriedItem(percentageRemaining : 100)

var pctLeft = item.percentageRemaining
pctLeft = 42

// PSEUDOCODE:
image.topMarginOffset = 100 - pctLeft

You could try adjusting the frame of the UIImageView based on the percentage left. 您可以尝试根据剩余百分比调整UIImageView的框架。

var image = UIImageView(frame: CGRect(x: 20, y: 10, width: 50, height: 100))
var pctLeft = 0.42

//assuming you want a vertical progress bar (like a glass of water).
image.frame = CGRect(x: 20, y: 10 + (100 * (1 - pctLeft)), width: 50, height: 100 * pctLeft)

Here I programmatically push the Y value down based on the reduced size of the bar. 在此,我根据条形的减小大小以编程方式将Y值向下推。 Let me know if this is what you were looking for... 让我知道这是否是您想要的...

暂无
暂无

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

相关问题 Swift-如何以编程方式旋转UITableViewHeader中的UIImageView - Swift - How to programmatically rotate a UIImageView in UITableViewHeader 如何在 Swift 中以编程方式调整 UILabel 行间距? - How to adjust a UILabel line spacing programmatically in Swift? Swift & UILabel:如何以编程方式在 Swift 中添加填充和边距? - Swift & UILabel : How to add padding and margin in Swift programmatically? 在Swift中以编程方式将UIImageView替换为PDFView - Programmatically replace UIImageView with PDFView in Swift 如何在Swift中使用UISlider在UIImageView中以编程方式更改指定的图像大小 - How to change specified image size in UIImageView with UISlider in Swift programmatically 如何以编程方式在 iOS 设备上的 swift 中以编程方式显示 UIImageView 后面的彩色磁盘 - how to programmatically show a colored disk behind a UIImageView, in swift on an iOS device 如何在UIButton中调整文本的字体大小以在Swift中以编程方式适应宽度? - How to adjust font size of text in a UIButton to fit the width programmatically in Swift? 如何调整JSQMessagesCollectionViewCell的气泡边距 - How to adjust the bubble margin of the JSQMessagesCollectionViewCell 以编程方式在UITableViewCell下添加UITextView和UIImageView Swift 4 - Add UITextView and UIImageView under UITableViewCell programmatically Swift 4 Swift-从程序设计约束中拉伸UIImageView - Swift - Stretched UIImageView from programmatically constraints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM