测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2015年11月 |
由 Andrea Mazzini 维护。
iOS 的滑出导航控制器。该控制器模仿了 Facebook 或 Steam 等应用程序的“滑出”导航行为。该项目当前模仿了 FB 的应用程序样式,您可以使用选项字典(请参阅以下文档)更改所有表格颜色和选项。仅支持 ARC。
部分代码基于 Nick Harris 的博客文章:iOS 滑出导航代码
SlideOut 导航显示的 ViewControllers 按照 UITableView 的部分和行进行排列。
您可以使用以下帮助方法来设置您的视图:
addSectionWithTitle:
添加部分addViewControllerToLastSection:tagged:withTitle:andIcon:
将视图控制器添加到最后一个部分addViewControllerToLastSection:tagged:withTitle:andIcon:beforeChange:onCompletion:
添加视图控制器并在视图切换前后调用块addViewController:tagged:withTitle:andIcon:toSection:
将视图控制器添加到特定部分addViewController:tagged:withTitle:andIcon:toSection:beforeChange:onCompletion:
将视图控制器添加到特定部分并在切换前后调用块创建新部分
// Note: Leave the title blank to hide the section header
[self.slideoutController addSectionWithTitle:@"Section"];
创建新行
[self.slideoutController addViewControllerToLastSection:controller // Your UIViewController
tagged:1 // Used to change the object's properties, i.e. the badge
withTitle:@"First View"
andIcon:@"icon1.png"]; // 44x44 icon held in the project's resources
创建仅调用操作的行
[self.slideoutController addActionToLastSection:^{
// some action
}
tagged:3
withTitle:@"Action"
andIcon:@""];
主要数据结构是部分数组。每个部分项是一个字典,包含部分标题和一个描述 ViewControllers 的数组。此数组中的每个项都是一个字典,包含标题、图标文件名、一个数字标记和 ViewController 的引用。您还可以将完整的数据结构传递给初始化方法,但不太推荐。请查看示例以了解这些方法的工作方式。
自 1.0.0 版本起,AMSlideOutNavigationController 的配置可以通过传递一个 NSDictionary 来处理。默认值可以在 AMSlideOutNavigationController.m 中找到。以下是可能的选项的简要描述
AMOptionsEnableGesture // @(BOOL), Enables the pan gesture. Defaults to YES
AMOptionsEnableShadow // @(BOOL), Enables the shadow under the content's view. Defaults to YES
AMOptionsSetButtonDone // @(BOOL), Sets the Menu button's state to 'Done' when the tray is open. Defaults to NO
AMOptionsUseBorderedButton // @(BOOL), Sets the Menu button to have a bordered style. Defaults to NO
AMOptionsButtonIcon // UIImage, Icon displayed in the Menu button. Defaults to the embedded 'iconSlide.png'
AMOptionsTableBackground // UIImage, Background image displayed and tiled as the TableView's background. Defaults to nil (solid color)
AMOptionsTableOffestY // @(float), Y offset for the menu table. Defaults to 20.0f in iOS7, 0.0f in previous versions.
AMOptionsUseDefaultTitles // @(BOOL), When enabled the content view's title is set as the manu item's text. Defaults to YES
AMOptionsSlideValue // @(float), The width of the menu. The content's view snaps to this value. Defaults to 270
AMOptionsBackground // UIColor, Menu's background color. Defaults to the one in the main screenshot of this page
AMOptionsSelectionBackground // UIColor, Menu background color for the selected item. Defaults to the one in the main screenshot of this page
AMOptionsHeaderFont // UIFont, Font used in the menu header. Defaults to Helvetica@13
AMOptionsHeaderFontColor // UIColor, Font color used in the menu header. Defaults to the one in the main screenshot of this page
AMOptionsHeaderShadowColor // UIColor, Shadow color used in the menu header. Defaults to the one in the main screenshot of this page
AMOptionsHeaderPadding // @(float), Padding used in the menu header. Defaults to 10
AMOptionsHeaderGradientUp // UIColor, Menu's gradient starting color. Defaults to the one in the main screenshot of this page
AMOptionsHeaderGradientDown // UIColor, Menu's gradient ending color. Defaults to the one in the main screenshot of this page
AMOptionsHeaderSeparatorUpper // UIColor, Color used in the menu header separator. Defaults to the one in the main screenshot of this page
AMOptionsHeaderSeparatorLower // UIColor, Color used in the menu header separator. Defaults to the one in the main screenshot of this page
AMOptionsCellFont // UIFont, Font used in the menu item. Defaults to Helvetica@14
AMOptionsCellFontColor // UIColor, Font color used in the menu item. Defaults to the one in the main screenshot of this page
AMOptionsCellBadgeFont // UIFont, Font used in the menu badge. Defaults to Helvetica@12
AMOptionsCellBadgeFontColor // UIColor, Badge's font color. Defaults to white
AMOptionsCellBadgeBackColor // UIColor, Badge's background color. Defaults to black
AMOptionsCellBackground // UIColor, Menu item background color. Defaults to the one in the main screenshot of this page
AMOptionsCellSeparatorUpper // UIColor, Color used in the menu separator. Defaults to the one in the main screenshot of this page
AMOptionsCellSeparatorLower // UIColor, Color used in the menu separator. Defaults to the one in the main screenshot of this page
AMOptionsCellShadowColor // UIColor, Shadow color used in the menu item. Defaults to the one in the main screenshot of this page
AMOptionsImagePadding // @(float), Padding used in the menu icon. Defaults to 50
AMOptionsImageLeftPadding // @(float), Left padding used in the menu icon for the image. Defaults to 0
AMOptionsTextPadding // @(float), Padding used in the menu item. Defaults to 20
AMOptionsBadgePosition // @(float), Badge's left offset. Defaults to 220
AMOptionsHeaderHeight // @(float), Menu header's height. Defaults to 22
AMOptionsImageHeight // @(float), Menu icon's height. Max value is currently 44 pixels. Defaults to 44
AMOptionsImageOffsetByY // @(float), Menu icon's offset from the top. Defaults to 0
AMOptionsAnimationShrink // @(BOOL), Enables the Shrink animation. Defaults to @YES
AMOptionsAnimationShrinkValue // @(float), The amount of scaling for the shrink animation. Defauults to @0.3
AMOptionsAnimationDarken // @(BOOL), Enables the fadout animation. Defaults to @YES
AMOptionsAnimationDarkenValue // @(float), The darker alpha value of the dark overlay. Defaults to @0.7
AMOptionsAnimationDarkenColor // UIColor, The base color of the fadout animation. Defaults to [UIColor blackColor]
AMOptionsAnimationSlide // @(BOOL), Enables the slide animation of the menu to the side. Defaults to @NO
AMOptionsAnimationSlidePercentage // @(float), Value from 0 to 1. Determines how much the table should slide aside
AMOptionsTableHeaderClass // NSString, The class name of your custom header. Defaults to AMSlideTableHeader
AMOptionsDisableMenuScroll // @(BOOL), Prevents the menu scrolling if its content's height is less than the view's height. Defaults to @NO
AMOptionsTableCellClass // NSString, The class name of your custom cell. It must inherit from AMSlideTableCell. Defaults to AMSlideTableCell
AMOptionsTableCellHeight // @(float), The default cell's height. Defaults to @44
AMOptionsTableIconMaxSize // @(float), the default menu item's icon. Defaults to @44
AMOptionsSlideoutTime // @(float), The default duration of the open/close animation. Defaults to @0.15
AMOptionsTableBadgeHeight // @(float), The table badge height. Defaults to @20
AMOptionsSlideShadowOffset // @(float), The shadow's offset under the content controller. Defaults to -6
AMOptionsSlideShadowOpacity // @(float), The shadow's opacity under the content controller. Defaults to 0.4
AMOptionsBadgeShowTotal // @(BOOL=), Enables the automatic global badge. Defaults to @NO
AMOptionsBadgeGlobalFont // @(UIFont), The global badge's font. Defaults to [UIFont systemFontOfSize:8]
AMOptionsBadgeGlobalPositionX // @(float), The global bages's X position (relative to the bar button). Defaults to @20
AMOptionsBadgeGlobalPositionY // @(float), The global bages's Y position (relative to the bar button). Defaults to @(-5)
AMOptionsBadgeGlobalPositionW // @(float), The global bages's width. Defaults to @16
AMOptionsBadgeGlobalPositionH // @(float), The global bages's height. Defaults to @16
AMOptionsBadgeGlobalTextColor // @(UIColor), The glboal badge's text color. Defaults to white
AMOptionsBadgeGlobalBackColor // @(UIColor), The glboal badge's background color. Defaults to red
AMOptionsBadgeGlobalShadowColor // @(UIColor), The glboal badge's text shadow color. Defaults to a clear color
AMOptionsNavbarTranslucent // @(BOOL), The navbar translucent state. Defaults to @NO
AMOptionsContentInsetTop // @(float), The top content inset of the table view. Defaults to @0
示例使用
self.slideoutController = [AMSlideOutNavigationController slideOutNavigation];
[self.slideoutController setSlideoutOptions:@{
AMOptionsEnableShadow : @(NO),
AMOptionsHeaderFont : [UIFont systemFontOfSize:14]
}];
要自定义您的导航栏的外观,您可以创建系统的 UIAppearance 类,例如:
// Navbar customization
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
// Navbar font customization
NSDictionary *attributes = @{UITextAttributeFont: [UIFont fontWithName:@"Copperplate" size:14]};
NSDictionary *titleAttributes = @{UITextAttributeFont: [UIFont fontWithName:@"Copperplate" size:20]};
[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
forState: UIControlStateNormal];
[[UINavigationBar appearance] setTitleTextAttributes: titleAttributes];
// Back button customization
UIImage *barButton = [[UIImage imageNamed:@"button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(-2,5,0,6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButton forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:4 forBarMetrics:UIBarMetricsDefault];
使用 UIAppearance 和选项字典,您可以轻松地实现与页面上第二张截图相似的外观。
self.slideoutController = [AMSlideOutNavigationController slideOutNavigation];
// Add a first section
[self.slideoutController addSectionWithTitle:@"Section One"];
// Add two viewcontrollers to the first section
controller = [[UIViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self.slideoutController addViewControllerToLastSection:controller tagged:1 withTitle:@"First View" andIcon:@"icon1.png"];
controller = [[UIViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.slideoutController addViewControllerToLastSection:controller tagged:2 withTitle:@"Second View" andIcon:@"icon2.png"];
// Add a second section
[self.slideoutController addSectionWithTitle:@"Section Two"];
// Add two viewcontrollers to the second section
controller = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self.slideoutController addViewControllerToLastSection:controller
tagged:3
withTitle:@"First View"
andIcon:@"icon1.png"
beforeChange:^{
NSLog(@"Changing viewController");
} onCompletion:^{
NSLog(@"Done");
}];
controller = [[UIViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.slideoutController addViewControllerToLastSection:controller tagged:4 withTitle:@"Second View" andIcon:@"icon2.png"];
[self.window setRootViewController:self.slideoutController];
如果您使用场景图,则可以轻松地将AMSlideOutNavigationController集成到您的AppDelegate中。
只需为将成为导航树根元素的每个ViewController设置一个场景图ID
然后按以下方式在AppDelegate中实例化您的ViewController
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController* controller;
self.slideoutController = [AMSlideOutNavigationController slideOutNavigation];
[self.slideoutController addSectionWithTitle:@""];
controller = [storyboard instantiateViewControllerWithIdentifier:@"FirstController"];
[self.slideoutController addViewControllerToLastSection:controller tagged:1 withTitle:@"First View" andIcon:@""];
controller = [storyboard instantiateViewControllerWithIdentifier:@"SecondController"];
[self.slideoutController addViewControllerToLastSection:controller tagged:2 withTitle:@"Second View" andIcon:@""];
[self.window setRootViewController:self.slideoutController];
1.6+: 将部分名称的值设为nil,如果不想显示部分标题。您可以指定每个部分的自定义标题类和高度,这将为您提供添加一个不同于标题的分离器的机会。您还可以定义一个全局自定义标题类。在1.6之前:如果您不想显示部分标题,请留空部分名称。
使用方法setBadgeValue:forTag:
设置徽章值(NSString)。这将设置在视图设置期间提供的带数字标签的行的徽章值。如果启用了AMOptionsBadgeShowTotal,则全局徽章将在左侧工具栏按钮上显示。
[self.slideoutController setBadgeValue:@"10" forTag:3];
您可以使用以下方式设置自定义全局徽章:
[self.slideoutController setBadgeTotalValue:@"10"];
图标大小必须为44x44。留空图标名称将导致只有文本的行,具有不同的缩进。
setMenuScrollingEnabled
方法添加了通过编程切换控制器并在其后执行操作的方法。
- (void)switchToControllerTagged:(int)tag andPerformSelector:(SEL)selector withObject:(id)obj
请让我知道!我将很高兴在此处链接您的项目。
Copyright (c) 2013 Andrea Mazzini. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.