我有一个 ScrollView ,可以容纳一些我从网上下载的图片。我将这些图像添加到 ScrollView 中,但在它们之间添加黑色空间时遇到了问题。它们是垂直滚动的,而不是水平的。我一直在考虑尝试添加一个空格,但不知道该怎么做。任何帮助,将不胜感激。 这是我目前所拥有的;
private func loadImages() {
myImages = 0;
ImageManager.sharedInstance.getAllImagesFromServer({
promotions in
for (_,promotion) in promotions {
Utility.log(self.dynamicType, msg: promotion.picture, function: "viewDidLoad")
self.downloadImage(NSURL(string: promotion.picture)!)
}},
onFail: { error_code, detail in Utility.log(self.dynamicType, msg: "Get All Iamges Fail, error_code: \(error_code), detail: \(detail)",
function: "viewDidLoad")})
}
func getMyImage(url: NSURL){
getDataFromUrl(url) { (data, response, error) in
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard let data = data where error == nil else { return }
print(response?.suggestedFilename ?? "")
let imageView = UIImageView(image: UIImage(data: data));
imageView.frame = CGRect(x: 0,
y: Int(self.myImages * 200), width: Int(self.myScrollView.frame.size.width), height: 200)
self.myScrollView.addSubview(imageView)
self.myImages = self.myImages + 1;
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width,
CGFloat(self.myImages * 200));
}
}
}
最佳答案
在图像之间添加一些空间。如果您的图片有 200 点高,请将教学图片的 y 位置设置为 Int(self.myImages * 210)
。
或者更好的是,用图像高度常数来表示:
let imageHeight = 200
let imageWidth = 200
let image = UIImage(data: data)
let imageView = UIImageView(image: image);
imageView.frame = CGRect(
x: 0,
y: Int(self.myImages * imageHeight + 10),
width: Int(self.myScrollView.frame.size.width),
height: imageHeight)
我不知道 ScrollView 的背景默认是白色还是黑色。您可能必须将 ScrollView 的 contentView 的 backgroundColor 设置为黑色。
关于ios - 在 ImageView 中的图像之间添加黑色空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37780442/