【原】自定義UINavigationItem的兩種方法以及相應的隱藏方法


第一種:

UIImage *searchimage=[UIImage imageNamed:@"search.png"];

    UIBarButtonItem *barbtn=[[[UIBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStyleDone target:self action:@selector(searchprogram)] autoRelease];    

    barbtn.image=searchimage;

    self.navigationItem.rightBarButtonItem=barbtn;

這種設置出來的item圖片跟大小是固定的

其隱藏方法是:在需要隱藏的時候self.navigationItem.xxxItem = nil;

顯示方法是重新alloc-init一次;

第二種: 

IButton*rightButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,30,30)];

    [rightButtonsetImage:[UIImageimageNamed:@"search.png"]forState:UIControlStateNormal];

    [rightButtonaddTarget:selfaction:@selector(searchprogram)forControlEvents:UIControlEventTouchUpInside];

   UIBarButtonItem*rightItem = [[UIBarButtonItemalloc]initWithCustomView:rightButton];

    [rightButton release];

   self.navigationItem.rightBarButtonItem= rightItem;

    [rightItem release];

這種方式設計出來的item比較靈活,尤其是在 隱藏顯示的時候:

隱藏方法:self.navigationItem.xxxItem.customView.hidden =YES;

顯示方法,同上xxx.hidden = NO;

這種方式更合理一些,因為不需要不斷的創建/消除,所以推薦用這種方法!

第三種:

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(methodtocall:) ]; 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM