【问题标题】:Add black spaces between images in a image view在图像视图中的图像之间添加黑色空间
【发布时间】:2016-06-13 00:34:53
【问题描述】:

我有一个滚动视图,其中包含我从网上下载的一些图像。我将这些图像添加到滚动视图中,但在它们之间添加黑色空间时遇到问题。它们处于垂直滚动而不是水平滚动中。我一直在考虑尝试添加一个空间,但不知道该怎么做。任何帮助,将不胜感激。 这是我到目前为止所拥有的;

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));
        }
    }
}

【问题讨论】:

  • 只需创建每个帧的y 值。

标签: ios uiscrollview uiimageview


【解决方案1】:

在图像之间添加一些空间。如果您的图像高 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)

我不知道默认情况下滚动视图的背景是白色还是黑色。您可能必须将滚动视图的 contentView 的 backgroundColor 设置为黑色。

【讨论】:

  • 谢谢你完美的工作。我在 android 上做到了这一点,但我对 swift 的了解足以让我陷入困境。我很感激
猜你喜欢
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多