iOS uploads images--dynamic layouts based on the number of images

Source: Internet
Author: User

In iOS development, most of the software should be useful to upload images (chat, post, comment, etc.), such as

I will simply say similar image upload automatic layout features:

1. Show only one placeholder image by default

  

2. Change the position of the placeholder image according to the number of pictures selected

  

3. Once the number of pictures is more than one line, the page adapts automatically and becomes 2 lines.

  

4. Click on the image and click on the "+" number placeholder image method is different

Click image to enlarge image

Click on the "+" number Picture:

    

5. Display up to 9 pictures, when 9 is displayed, the placeholder image "+" is not displayed

  

    

Above the basic function of the picture automatic layout, the following explains how to achieve

1. Create a uiview to hold the selected photos

2. Simple default start directly create 9 buttons (picture placed on the button), and add click events for the button, and then all add to UIView

    

3. In this case, the default display of 9 buttons, 9 pictures, does not meet our automatic layout requirements, so the height of the UIView based on the number of selected images to achieve adaptive

(Note: Multiple photo selection can be used zyqassetpicker frame, the students need to leave a message, or GitHub above to find)

-->a. All we first hide all the first steps created by BTN

B. Set the height constraint based on the number of pictures selected

      

4. Once you know the height of the display, you need to know the number of buttons to display

A. Because of the first step, we add 9 btn to the UIView, all of which can be removed according to the subview subscript.

  

    

B. Judging by the number of photos selected (if <9, also display a placeholder picture "+" picture)

  

5. Set the click events for different buttons

A. If it is a picture, display--Enlarge the picture

B. If it is the default placeholder picture, display "Upload picture"

      

Analysis: In fact, whether it is a or B, are image settings on the btn display, the essence is the same!!

To distinguish: is not the picture display is ImageView, "+" button is BTN

    

Workaround: Pass the button's subscript!

If 3 pictures, then (0,1,2) are called to enlarge the image method--a, Subscript 3 (fourth) is--b

    

  

The basic process is like the previous steps, of course, there are some details that need to be added, such as the deletion of images, 4 pictures to display 2 lines (a default "+" placeholder picture), if you delete a picture at this time, the remaining 3 pictures, then only one line, and then refresh the uiview height constraints, If there is just the need to do this function of small white children's shoes, you can leave a message or private me.

iOS uploads images--dynamic layouts based on the number of images

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.