IOS get icon position for Uitabviewcontroller and Uinavigationcontroller

Source: Internet
Author: User

These icons are placed in Uitabbar and Uinavigationbar. So just go through their subviews and find the type is UIButton.

It's easy to get the relative position of all of them.

But I have a need to cover the entire screen with a layer of UIView, and then draw some patterns next to these icons (I'm actually doing the tutorial). At this point, you need to get their position in the entire view relative to the outermost uiviewcontroller. It's not that easy.

In my application, the outermost layer has a uitabbarcontroller, nested inside a uinavigationcontroller, nested inside a uiviewcontroller. Since the uitabbarcontroller is at the outermost layer, the position acquired is the desired position. And the icons in the Uinavigationbar need to be adjusted. That is, you need to join Uitabbarcontroller's layout margin. Tested, the average size of the iphone this value is 16, and the iphone 6plus,ipad this value of 20.

So the position I want is:

1. Icons on the left

Gets the relative position of the +layoutmargin.left

2. Icons on the right

Gets the relative position of the-layoutmargin.right

Get the code for the relative position:

+ (Cgpoint) Centerfortabintabbar: (uitabbar*) TabBar Withindex: (nsuinteger) index{Nsmutablearray*tabbaritems =[Nsmutablearray arraywithcapacity:[tabbar.items Count]];  for(UIView *viewinchtabbar.subviews) {if([View iskindofclass:nsclassfromstring (@"Uitabbarbutton")] &&[View Respondstoselector: @selector (frame)]) {            //Check for the Selector-frame-prevent crashes in the very unlikely case, the future//objects thar don ' t implement-frame can be subviews of an UIView[Tabbaritems Addobject:view]; }    }    if([tabbaritems count] = =0) {        //no tabbaritems means either no uitabbarbuttons were in the SubView, or none responded To-frame//return Cgrectzero to indicate so we couldn ' t figure out the frame        returnCgpointzero; }        //Sort by origin.x of the frame because the items is not necessarily in the correct order[Tabbaritems Sortusingcomparator:^nscomparisonresult (UIView *view1, UIView *view2) {        if(View1.frame.origin.x <view2.frame.origin.x) {returnnsorderedascending; }        if(View1.frame.origin.x >view2.frame.origin.x) {returnnsordereddescending; } nsassert (NO,@"%@ and%@ share the same origin.x. This should never happen and indicates a substantial change in the framework of that renders this method useless.", View1, VIEW2); returnNsorderedsame;        }]; Cgpoint Center=Cgpointzero; if(Index <[Tabbaritems Count]) {        //Viewcontroller is in a regular tabUIView *tabview =Tabbaritems[index]; Center=Tabview.center; }    Else {        //Our target viewcontroller are inside the "more" tabUIView *tabview =[Tabbaritems Lastobject]; Center=Tabview.center; }    returnCenter;}

+ (Cgpoint) Centerforiteminnavigationbar: (Uinavigationbar *) Navigationbar Withindex: (nsuinteger) index{Nsmutablearray*navigationbaritems =[Nsmutablearray arraywithcapacity:[navigationbar.items Count]];  for(UIView *viewinchnavigationbar.subviews) {if([View iskindofclass:nsclassfromstring (@"UIButton")] &&[View Respondstoselector: @selector (frame)]) {            //Check for the Selector-frame-prevent crashes in the very unlikely case, the future//objects thar don ' t implement-frame can be subviews of an UIView[Navigationbaritems Addobject:view]; }    }    if([navigationbaritems count] = =0) {        //no tabbaritems means either no uitabbarbuttons were in the SubView, or none responded To-frame//return Cgrectzero to indicate so we couldn ' t figure out the frame        returnCgpointzero; }        //Sort by origin.x of the frame because the items is not necessarily in the correct order[Navigationbaritems Sortusingcomparator:^nscomparisonresult (UIView *view1, UIView *view2) {        if(View1.frame.origin.x <view2.frame.origin.x) {returnnsorderedascending; }        if(View1.frame.origin.x >view2.frame.origin.x) {returnnsordereddescending; } nsassert (NO,@"%@ and%@ share the same origin.x. This should never happen and indicates a substantial change in the framework of that renders this method useless.", View1, VIEW2); returnNsorderedsame;        }]; Cgpoint Point=Cgpointzero; if(Index <[Navigationbaritems Count]) {        //Viewcontroller is in a regular tabUIView *navigationview =Navigationbaritems[index]; if([Navigationview respondstoselector: @selector (frame)]) { point=Navigationview.center;        }    }    Else {        //Our target viewcontroller are inside the "more" tabUIView *navigationview =[Navigationbaritems Lastobject]; if([Navigationview respondstoselector: @selector (frame)]) { point=Navigationview.center; }    }    returnPoint ;}

IOS get icon position for Uitabviewcontroller and Uinavigationcontroller

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.