The magical kvo mechanism of iOS development

Source: Internet
Author: User

Suppressed excited mood, taking advantage of today's time, I will all take to write blog well, of course, do not blow the big. Blow more, can't write code how to do it.


Preface: Remember when I received their first project, the home of the CollectionView frame has been set not good, always appear collectionview too big, or the bottom of the ScrollView contentsize too small. These can be achieved through a minor frame without blocking. But then, when the customer offered to add the Hot recommendation field, I lost it. How to do it, and also to ensure that there is------END-----field at the bottom of the CollectionView. These can not be achieved through a minor frame. Finally, learn from the predecessors of the method, with the KVO mechanism, skillfully solve the problem.



Body:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/77/ED/wKioL1ZxOJSAzx7NAAEBypBKA_U874.png "style=" float: none; "title=" Hot sale recommended. png "alt=" Wkiol1zxojsazx7naaebypbka_u874.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/77/ED/wKioL1ZxOJez2A6PAABMWY7EYNk220.png "style=" float: none; "title=" End.png "alt=" Wkiol1zxojez2a6paabmwy7eynk220.png "/>

PS: Younger brother just write, do not know how to adjust the size, hope forgive me, forgive me ha.

It should not be difficult to see the size of the CollectionView in the figure of the dynamic change it.

Here, when we are ready to add the CollectionView view to the Self.view, we can use the Interfacebuilder to draw the hot and end fields in the diagram. Then pull a collectionview in the middle of the two words to set his restraint.

Then in our implementation code file, initialize an observer in Viewwillappear:

[contentcollectionview addobserver: self forkeypath: @ "contentsize" options:nskeyvalueobservingoptionnew context: @ "recommendproductcontentsize"];

Then remove the observer in the Viewwilldisappear:

if (Isrem) {

@try {

[contentcollectionview removeobserver: self forkeypath : @ "contentsize" ];

}

@catch (NSException *exception) {

NSLog (@ "%@", exception);

}

@finally {

}

}


Then implement the callback callback method:

-(void) Observevalueforkeypath: (nsstring *) keypath ofobject: (ID) object change: ( Nsdictionary<nsstring *,ID> *) Change context: (void *) Context {

if ([KeyPath isequaltostring:@ "Contentsize"]) {

cgsize size;

[change[@ "new"] getValue:&size];

Recommendationview. frame = cgrectmake(0, 370+4, Centerview. Bounds. size. width, size. height+122);

        ScrollView.contentsize = Cgsizemake(Centerview.Frame.size.width, Recommendationview.Frame.Origin.y+Recommendationview.Frame.size.Height);

}Else{

NSLog (@ " add Observer error ");

}

}

In this case we set up our Hot recommendation field frame with ScrollView's contentsize.

Size is the height of the CollectionView after the change.

So that we can achieve the effect we want to achieve.

The magical kvo mechanism of iOS development

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.