在侧边栏上方添加空间

Posted

技术标签:

【中文标题】在侧边栏上方添加空间【英文标题】:Add Space Above SideBar 【发布时间】:2013-10-27 04:02:45 【问题描述】:

我有一个侧边栏,我使用它来实现: http://mobile.tutsplus.com/tutorials/iphone/implementing-container-containment-sliding-menu-controller/

但是,我注意到第一个菜单项上方没有足够的空间,您可以在此处看到:

这是我的 rootcontroller.m 中的代码

#define kExposedWidth 200.0
#define kMenuCellID @"MenuCell"

#import "RootController.h"

@interface RootController()

@property (nonatomic, strong) UITableView *menu;
@property (nonatomic, strong) NSArray *viewControllers;
@property (nonatomic, strong) NSArray *menuTitles;

@property (nonatomic, assign) NSInteger indexOfVisibleController;

@property (nonatomic, assign) BOOL isMenuVisible;

@end


@implementation RootController

- (id)initWithViewControllers:(NSArray *)viewControllers andMenuTitles:(NSArray *)menuTitles

    if (self = [super init])
    
        NSAssert(self.viewControllers.count == self.menuTitles.count, @"There must be one and only one menu title corresponding to every view controller!");    // (1)
        NSMutableArray *tempVCs = [NSMutableArray arrayWithCapacity:viewControllers.count];

        self.menuTitles = [menuTitles copy];

        for (UIViewController *vc in viewControllers) // (2)
        
            if (![vc isMemberOfClass:[UINavigationController class]])
            
                [tempVCs addObject:[[UINavigationController alloc] initWithRootViewController:vc]];
            
            else
                [tempVCs addObject:vc];

            UIBarButtonItem *revealMenuBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStylePlain target:self action:@selector(toggleMenuVisibility:)]; // (3)

            UIViewController *topVC = ((UINavigationController *)tempVCs.lastObject).topViewController;
            topVC.navigationItem.leftBarButtonItems = [@[revealMenuBarButtonItem] arrayByAddingObjectsFromArray:topVC.navigationItem.leftBarButtonItems]; 


        
        self.viewControllers = [tempVCs copy];
        self.menu = [[UITableView alloc] init]; // (4)
        self.menu.delegate = self;
        self.menu.dataSource = self;

    
    return self;




- (void)viewDidLoad

    [super viewDidLoad];
    [self.menu registerClass:[UITableViewCell class] forCellReuseIdentifier:kMenuCellID];
    self.menu.frame = self.view.bounds;
    [self.view addSubview:self.menu];

    self.indexOfVisibleController = 0;
    UIViewController *visibleViewController = self.viewControllers[0];
    visibleViewController.view.frame = [self offScreenFrame];
    [self addChildViewController:visibleViewController]; // (5)
    [self.view addSubview:visibleViewController.view]; // (6)
    self.isMenuVisible = YES;
    [self adjustContentFrameAccordingToMenuVisibility]; // (7)


    [self.viewControllers[0] didMoveToParentViewController:self]; // (8)



- (void)toggleMenuVisibility:(id)sender // (9)

    self.isMenuVisible = !self.isMenuVisible;
    [self adjustContentFrameAccordingToMenuVisibility];



- (void)adjustContentFrameAccordingToMenuVisibility // (10)

    UIViewController *visibleViewController = self.viewControllers[self.indexOfVisibleController];
    CGSize size = visibleViewController.view.frame.size;

    if (self.isMenuVisible)
    
        [UIView animateWithDuration:0.5 animations:^
            visibleViewController.view.frame = CGRectMake(kExposedWidth, 0, size.width, size.height);
        ];
    
    else
        [UIView animateWithDuration:0.5 animations:^
            visibleViewController.view.frame = CGRectMake(0, 0, size.width, size.height);
        ];



- (void)replaceVisibleViewControllerWithViewControllerAtIndex:(NSInteger)index // (11)

    if (index == self.indexOfVisibleController) return;
    UIViewController *incomingViewController = self.viewControllers[index];
    incomingViewController.view.frame = [self offScreenFrame];
    UIViewController *outgoingViewController = self.viewControllers[self.indexOfVisibleController];
    CGRect visibleFrame = self.view.bounds;


    [outgoingViewController willMoveToParentViewController:nil]; // (12)

    [self addChildViewController:incomingViewController]; // (13)
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; // (14)
    [self transitionFromViewController:outgoingViewController // (15)
                      toViewController:incomingViewController
                              duration:0.5 options:0
                            animations:^
                                outgoingViewController.view.frame = [self offScreenFrame];

                            

                            completion:^(BOOL finished) 
                                [UIView animateWithDuration:0.5
                                                 animations:^
                                                     [outgoingViewController.view removeFromSuperview];
                                                     [self.view addSubview:incomingViewController.view];
                                                     incomingViewController.view.frame = visibleFrame;
                                                     [[UIApplication sharedApplication] endIgnoringInteractionEvents]; // (16)
                                ];
                                [incomingViewController didMoveToParentViewController:self]; // (17)
                                [outgoingViewController removeFromParentViewController]; // (18)
                                self.isMenuVisible = NO;
                                self.indexOfVisibleController = index;
   ];



// (19):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return self.menuTitles.count;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMenuCellID];
    cell.textLabel.text = self.menuTitles[indexPath.row];
    return cell;


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    [self replaceVisibleViewControllerWithViewControllerAtIndex:indexPath.row];


- (CGRect)offScreenFrame

    return CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height);


@end

【问题讨论】:

你是指"Carrier""Cheats"之间的空格吗? 是的,先生。我只需要添加一些空格。 【参考方案1】:

为您的部分标题指定高度,它将在第一行上方添加空间..

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
    return 40; //set this value

【讨论】:

添加到你的 RootController。【参考方案2】:

您真正的问题是处理应用顶部的状态栏。

您是否希望在您的应用正在使用时状态栏一直存在?那么您可能会考虑将整个界面降低 20 点(除非您真的希望状态栏与您的内容重叠)

或者,您可以在整个应用中隐藏状态栏,或者仅在特定的视图控制器中隐藏。

【讨论】:

以上是关于在侧边栏上方添加空间的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress:为特定模板添加小部件侧边栏

在页脚上重叠的滚动固定侧边栏

以编程方式将 UITableView 添加到侧边栏

VueJS将活动类添加到侧边栏链接

将文件夹添加到 CFileDialog 的侧边栏

如何将侧边栏添加到存档页面