IOS solves the problem of Uicollectionview computing Cell size _ios

Source: Internet
Author: User

Objective

API not familiar with the cause of the problem, take it for granted to understand that there will be problems, here to record the use of Uicollectionview.

Body

  Trap One: minimumlinespacing, minimuminteritemspacing

It's easy to set these two properties to 0, which are the minimum line spacing and the minimum column spacing, and note that the minimum!! That means you can actually > 0, not spacing is 0.

  Trap Two: Sectioninset

Sets the margins of the cell. At first I thought that is each cell's margin, the adjacent will overlay the effect, actually is not such, this attribute only guarantees the spacing between adjacent, does not stack!!

Understanding the above two traps, we can accurately calculate the size of the Cell, and then set the size of the itemsize will be correct. For example:

 Let item_min_width:cgfloat =
  item_spacing:cgfloat = 6

  func resizecollectionview (size:cgsize) {
    if L ET layout = CollectionView? Collectionviewlayout as? uicollectionviewflowlayout {
      layout.minimumlinespacing = item_spacing
      layout.minimuminteritemspacing = ITEM _spacing

      var itemCount = Int (size.width/item_min_width)
      if ItemCount = = 0 {
        ItemCount = 1
      }

      if ITEM Count = = 1 {
        layout.itemsize = Cgsizemake (size.width, Size.width * 10/16)
        Layout.sectioninset = Uiedgeinsetsma Ke (6, 0, 0, 0)
      } else {let
        width = (size.width-cgfloat (itemCount + 1)) * item_spacing)/cgfloat (ItemCount) C15/>layout.itemsize = Cgsizemake (width, Width * 10/16)
        Layout.sectioninset = Uiedgeinsetsmake (item_spacing, ITEM _spacing, 0, item_spacing)
      }
      CollectionView? Layoutifneeded ()
    }
  }

Code Description:

The size of the current view, the dynamic calculation of cell sizes, you can easily fit Iphone/ipad, a single column hides both sides of the margin, multiple columns show both sides of the gap.

The above is a friend of iOS Uicollectionview usage and description, hoping to help iOS development.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.