集合查看定位像instagram app swift

时间:2016-11-01 11:14:10

标签: ios swift xcode uiscrollview storyboard

我一直在编写应用程序,如学习swift和ios编程的Instagram。现在我创建了UIStackView作为顶部栏,以及3个发布请求和更改图像的底部按钮。

我遇到的问题 - 我无法在CollectionView上设置位置,宽度和删除单元格间距。我想要: 所有设备上的CollectionView全宽 图像之间的单元间距= 1 CollectionView应位于顶部条形和底部按钮全高度

之间

我现在有什么?

我的模拟器屏幕:http://prntscr.com/d1mmu1

我的故事板屏幕:http://prntscr.com/d1mnhr

我尝试用StoryBoard做这个,改变了所有的参数,但还没达到我的目标。 如果我为CollectionView添加相同的高度和应用程序的主视图它需要所有屏幕,滚动后它消失... 如何在其上设置位置和宽度,高度和单元格间距?

here is a code of my viewcontroller for CollectionView - 

// MARK: - UICollectionViewDataSource协议

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.imageView.image = UIImage(named: "main/p\(self.items[indexPath.item].code)/main/main.jpg")
    print("main_card_images/p\(self.items[indexPath.item].code)/main/main")
    return cell
}

// MARK: - UICollectionViewDelegate protocol

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
    print(self.items[indexPath.item])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let yourNextViewController = (segue.destination as! ViewControllerCard)
    let indexPath = collectionView.indexPath(for: sender as! UICollectionViewCell)
    yourNextViewController.mainCardImage = self.items[(indexPath?.item)!]
}

1 个答案:

答案 0 :(得分:1)

试试此代码。

注意:以下代码仅适用于在所有iPhone型号上测试的4列布局。如果您需要不同数量的列。您需要使用单元格的宽度和高度,或者查看我的其他答案{ {3}}

var BookView = Backbone.View.extend({

    template: _.template($('#viewBook').html()),

    events: {
        "click #save": "bookSave"
    },

    initialize: function() {
        this.listenTo(this.model, 'change', this.render);
    },

    render: function() {
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },

    bookSave: function() {
        this.model.set({author: "nobody"});
    }

});

输出:

Adjust cellsize for fit all screens?