淘寶下拉菜單的實現(xiàn)

1.pic.jpg

工程中有需要做成這種下拉效果的菜單,走了很多彎路,分享幾種解決方法:
1 xib
仔細(xì)觀察淘寶,發(fā)現(xiàn)它并不是一個UITableView啊,不可以滑動的。枉我還找了那么多第三方什么UITableView加在UIView上啊,廢了很多時間。如果是固定死的菜單直接xib就好了。

Paste_Image.png

2 方法2 找到合適的第三方:
KxMenu LKPopupMenu 之類的 CocoaChina Github Code4App 上有大把。

3 iOS 8 以后有系統(tǒng)自帶的方法。
3.1 自定義一個UITableView 用來展示下拉數(shù)據(jù)

#import "WMPopoverController.h"

@interface WMPopoverController ()
@property (nonatomic, strong) NSMutableArray *addArray;
@end

@implementation WMPopoverController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.scrollEnabled = NO;
    self.addArray = [[NSMutableArray alloc] initWithObjects:@"掃一掃",@"搜一搜", @"創(chuàng)建討論組",@"發(fā)送到電腦", @"面對面快傳",@"收錢", nil];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.addArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@", self.addArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger number = indexPath.row;
    switch (number) {
        case 0:
            NSLog(@"掃一掃");
            break;
            
        case 1:
            NSLog(@"搜一搜");
            break;
            
        default:
            break;
    }
    
}

//重寫preferredContentSize,返回popover的大小
- (CGSize)preferredContentSize {
    if (self.presentingViewController && self.tableView != nil) {
        CGSize tempSize = self.presentingViewController.view.bounds.size;
        tempSize.width = 150;
        CGSize size = [self.tableView sizeThatFits:tempSize];  //sizeThatFits返回的是最合適的尺寸,但不會改變控件的大小
        return size;
    }else {
        return [super preferredContentSize];
    }
}

@end

3.2 設(shè)置導(dǎo)航控制器的右點(diǎn)擊事件

- (void)viewDidLoad {
   [super viewDidLoad];
   // 右邊
   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightBarAction)];
}

3.3 點(diǎn)擊按鈕的調(diào)用方法

@interface ViewController ()<UIPopoverPresentationControllerDelegate>
/** WMPopoverController */
@property (nonatomic, strong) WMPopoverController *itemPopVC;

@end


// 菜單  點(diǎn)擊按鈕調(diào)用的方法
- (void)rightBarAction {
    
    self.itemPopVC = [[WMPopoverController alloc] init];
    self.itemPopVC.modalPresentationStyle = UIModalPresentationPopover;
    self.itemPopVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;
    //箭頭方向
    self.itemPopVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    //代理
    self.itemPopVC.popoverPresentationController.delegate = self;
    [self presentViewController:self.itemPopVC animated:YES completion:nil];
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    NSLog(@"%@",controller);
    return  UIModalPresentationNone;
}

但是實現(xiàn)起來的效果箭頭圓滑并且顏色虛化。

Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容