分享

IOS App常用界面结构解析,让开发更简单

 BOBD1997 2019-03-23

https://blog.csdn.net/shenjie12345678/article/details/51254849

移动APP现在发展的如火如荼,各大应用商店都涌现了一大批优秀的app产品,但是作为一名app的消费者,以及app开发工程师,我觉得今天有必要在这里和大家一起来探讨一下如何实现一个简单的app开发过程,或者说一个app的结构该大致怎么实现。

在市面上,我们所使用的大部分工具应用类型的app都是有一定的界面结构的(类似淘宝,QQ, 微信),其中最主要的界面结构归纳起来就是使用 “导航栏(navigationBar) + 主视图(mainView)+工具栏(tabBar)”来实现,如图所示:

           

今天,就来讲一下,如何实现一个简单的应用型app最主要的界面UI框架。在这里我把这个框架拆分为几个部分,这样既有利于大家的理解也体现低耦合的特性(因为本身这几个自定义控件都是独立的,交互都通过接口来实现)。

如上图所示,这个界面包含了NavigationBar ,  UIViewController,  以及tabBarController;导航栏navigationbar顾名思义,当然是为了满足用户跳转与返回的操作;UIViewController的作用即是用于呈现给用户所需要看的内容;tabBarController的作用是用于管理多个控制器,轻松的完成视图之间的切换。

我们来简单的了解一下它的view层级图:

第一层是我自定义的一个导航栏CustomNavigationController继承自UINavigationController;我们通常把它作为整个程序的rootViewController,在AppDelegate中的applicationDidFinishLaunching函数中设置为根视图。

第二层视图CustomTabBarController(继承自UIViewController)是在CustomNavigationController初始化时,通过初始化API函数initWithRootViewController附加在导航栏上的,其作用是添加tabBar控件以及其他的UiviewController用于显示。

第三层视图CustomTabBar,继承自UIView; 是我们自定义的UITabBar控件,上面显示的每一个tabItem都对应着一个viewController;tabItem是自定义的按钮继承自UIButton,在下面的讲解中,我会主要介绍这些控件该如何实现。

创建自定义的CustomTabBarController

1.设置CustomTabBar控件的frame大小以及显示内容页面的frame大小

CustomTabBarController中声明了两个变量,一个是CustomTabBar对象(自定义的UITabBar),另一个是UIView对象,在界面进入viewWillAppear的时候初始化这个

两个控件,代码如下:

  1. - (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated{
  2. _tabBarHidden = hidden;
  3. __weak CustomTabBarController *weakSelf = self;
  4. void (^block)() = ^{
  5. CGSize viewSize = weakSelf.view.frame.size;
  6. CGFloat tabBarStartingY = viewSize.height;
  7. CGFloat contentViewHeight = viewSize.height;
  8. CGFloat tabBarHeight = CGRectGetHeight([[weakSelf tabBar] frame]);
  9. if (!tabBarHeight) {
  10. tabBarHeight = 55;
  11. }
  12. if (![weakSelf parentViewController]) {
  13. if (UIInterfaceOrientationIsLandscape([weakSelf interfaceOrientation])) {
  14. viewSize = CGSizeMake(viewSize.height, viewSize.width);
  15. }
  16. }
  17. if (!hidden) {
  18. tabBarStartingY = viewSize.height - tabBarHeight;
  19. // if (![[weakSelf tabBar] isTranslucent]) {
  20. contentViewHeight -= ([[weakSelf tabBar] minimumContentHeight] ?: tabBarHeight);
  21. // }
  22. [[weakSelf tabBar] setHidden:NO];
  23. }
  24. [[weakSelf tabBar] setFrame:CGRectMake(0, tabBarStartingY, viewSize.width, tabBarHeight)];
  25. [[weakSelf contentView] setFrame:CGRectMake(0, 0, viewSize.width, contentViewHeight)];
  26. };
  27. void (^completion)(BOOL) = ^(BOOL finished){
  28. if (hidden) {
  29. [[weakSelf tabBar] setHidden:YES];
  30. }
  31. };
  32. if (animated) {
  33. [UIView animateWithDuration:0.24 animations:block completion:completion];
  34. } else {
  35. block();
  36. completion(YES);
  37. }
  38. }

2.设置每个tabBarItem对应的UIViewController

在XCode工程中我新建了四个UIViewController对象,分别是FirstViewController, SecondViewController, ThirdViewController, 以及FourthViewController。这四个视图对象想分别与四个tabBarItem对应,我们调用函数setShowViewController来实现,代码如下:

  1. - (void)setShowViewControllers:(NSMutableArray *)mviewControllers{
  2. self.viewControllers = mviewControllers;
  3. if (mviewControllers && [mviewControllers isKindOfClass:[NSArray class]]) {
  4. self.viewControllers = mviewControllers;
  5. NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
  6. for (UIViewController *viewController in mviewControllers) {
  7. CustomTabBarItem *tabBarItem = [[CustomTabBarItem alloc] init];
  8. [tabBarItem setTitle:viewController.title forState:UIControlStateNormal];
  9. [tabBarItems addObject:tabBarItem];
  10. }
  11. [self.tabBar setTabBarItems:tabBarItems];
  12. } else {
  13. // for (UIViewController *viewController in _viewControllers) {
  14. // [viewController Custom_setTabBarController:nil];
  15. // }
  16. self.viewControllers = nil;
  17. }
  18. }


3.设置当前要显示的页面

承接上面的功能,既然我们的tabBarItem每一个都对应一个UIViewController,那如何实现让每一次的点击按钮过后,我们的界面就能跳转显示为正确的呢,设置的代码如下:

  1. //设置当前显示的页面
  2. - (void)setContentViewIndex:(NSInteger)index{
  3. self.selectedIndex = index;
  4. if(index >= self.viewControllers.count){
  5. return;
  6. }
  7. if([self selectedViewController]){
  8. [[self selectedViewController] willMoveToParentViewController:nil];
  9. [[[self selectedViewController] view] removeFromSuperview];
  10. [[self selectedViewController] removeFromParentViewController];
  11. }
  12. [[self tabBar] setSelectedItem:[[self tabBar] items][self.selectedIndex]];
  13. [self setSelectedViewController:[[self viewControllers] objectAtIndex:self.selectedIndex]];
  14. [self addChildViewController:[self selectedViewController]];
  15. [[[self selectedViewController] view] setFrame:[[self contentView] bounds]];
  16. [[self contentView] addSubview:[[self selectedViewController] view]];
  17. [[self selectedViewController] didMoveToParentViewController:self];
  18. }

创建自定义的CustomTabBar

TabBar在app中可谓是个非常重要的常客,为什么说他重要呢,因为它相当于是打开一个app里面所有功能的钥匙;tabBar中的每一个tabBarItem都对应

一个viewController, 通过触发按钮事件我们可以切换不同的页面。

1.设置tabBarItems

tabBar可不能没有tabBarItem, 通过头文件中提供的接口setTabBarItems,可以将app所需要的tabBarItem对象设置好,代码如下:

  1. - (void)setTabBarItems:(NSArray *)m_array{
  2. for (CustomTabBarItem *item in m_array) {
  3. [item removeFromSuperview];
  4. }
  5. self.items = m_array;
  6. for (CustomTabBarItem *item in m_array) {
  7. NSLog(@"%@", item);
  8. [item addTarget:self action:@selector(tabBarItemWasSelected:) forControlEvents:UIControlEventTouchDown];
  9. [self addSubview:item];
  10. }
  11. }

2.设置界面切换代理 

我们将显示当前所需界面的函数写在了CustomTabBarController这个类中,而我们的点击事件则是在CustomTabBar中,那如何才能调用到设置当前页面的函数呢!

我们这边就采用了代理delegate的模式,代码如下:

  1. @protocol CustomTabBarDelegate <NSObject>
  2. - (BOOL)tabBar:(CustomTabBar *)tabBar shouldSelectItemAtIndex:(NSInteger)index;
  3. - (void)tabBar:(CustomTabBar *)tabBar didSelectItemAtIndex:(NSInteger)index;
  4. @end

3.设置tabBarItem点击事件

因为我们的tabBarItem是继承自UIButton,所以这边用addtarget的方式为每一个item都添加了事件响应机制。代码如下:

  1. - (void)tabBarItemWasSelected:(id)sender {
  2. if ([[self delegate] respondsToSelector:@selector(tabBar:shouldSelectItemAtIndex:)]) {
  3. NSInteger index = [self.items indexOfObject:sender];
  4. if (![[self delegate] tabBar:self shouldSelectItemAtIndex:index]) {
  5. return;
  6. }
  7. }
  8. [self setSelectedItem:sender];
  9. if ([[self delegate] respondsToSelector:@selector(tabBar:didSelectItemAtIndex:)]) {
  10. NSInteger index = [self.items indexOfObject:self.selectedItem];
  11. [[self delegate] tabBar:self didSelectItemAtIndex:index];
  12. }
  13. }

创建自定义CustomTabBarItem

关于自定义的按钮,我在之前的博客中有写过一篇如何绘制一个精美的自定义的按钮大家感兴趣的话可以去看下;我先把这次功能的代码贴出来,可以先看一下:

  1. #import <UIKit/UIKit.h>
  2. @interface CustomTabBarItem : UIButton
  3. @property CGFloat itemHeight;
  4. #pragma mark - Title configuration
  5. @property (nonatomic, copy) NSString *title;
  6. @property (nonatomic) UIOffset titlePositionAdjustment;
  7. @property (copy) NSDictionary *unselectedTitleAttributes;
  8. @property (copy) NSDictionary *selectedTitleAttributes;
  9. #pragma mark - Image configuration
  10. @property (nonatomic) UIOffset imagePositionAdjustment;
  11. - (UIImage *)finishedSelectedImage;
  12. - (UIImage *)finishedUnselectedImage;
  13. - (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage;
  14. #pragma mark - Background configuration
  15. - (UIImage *)backgroundSelectedImage;
  16. - (UIImage *)backgroundUnselectedImage;
  17. - (void)setBackgroundSelectedImage:(UIImage *)selectedImage withUnselectedImage:(UIImage *)unselectedImage;
  18. #pragma mark - Badge configuration
  19. @property (nonatomic, copy) NSString *badgeValue;
  20. @property (strong) UIImage *badgeBackgroundImage;
  21. @property (strong) UIColor *badgeBackgroundColor;
  22. @property (strong) UIColor *badgeTextColor;
  23. @property (nonatomic) UIOffset badgePositionAdjustment;
  24. @property (nonatomic) UIFont *badgeTextFont;
  25. @end

  1. #import "CustomTabBarItem.h"
  2. @interface CustomTabBarItem () {
  3. NSString *_title;
  4. UIOffset _imagePositionAdjustment;
  5. NSDictionary *_unselectedTitleAttributes;
  6. NSDictionary *_selectedTitleAttributes;
  7. }
  8. @property UIImage *unselectedBackgroundImage;
  9. @property UIImage *selectedBackgroundImage;
  10. @property UIImage *unselectedImage;
  11. @property UIImage *selectedImage;
  12. @end
  13. @implementation CustomTabBarItem
  14. - (id)initWithFrame:(CGRect)frame {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. [self commonInitialization];
  18. }
  19. return self;
  20. }
  21. - (id)init {
  22. return [self initWithFrame:CGRectZero];
  23. }
  24. - (void)commonInitialization {
  25. // Setup defaults
  26. [self setBackgroundColor:[UIColor clearColor]];
  27. _title = @"";
  28. _titlePositionAdjustment = UIOffsetZero;
  29. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
  30. _unselectedTitleAttributes = @{
  31. NSFontAttributeName: [UIFont systemFontOfSize:10],
  32. NSForegroundColorAttributeName:[UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0f],
  33. };
  34. } else {
  35. #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
  36. _unselectedTitleAttributes = @{
  37. UITextAttributeFont: [UIFont systemFontOfSize:10],
  38. UITextAttributeTextColor: [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0f],
  39. };
  40. #endif
  41. }
  42. _selectedTitleAttributes = [_unselectedTitleAttributes copy];
  43. _badgeBackgroundColor = [UIColor redColor];
  44. _badgeTextColor = [UIColor whiteColor];
  45. _badgeTextFont = [UIFont systemFontOfSize:12];
  46. _badgePositionAdjustment = UIOffsetZero;
  47. }
  48. - (void)drawRect:(CGRect)rect {
  49. CGSize frameSize = self.frame.size;
  50. CGSize imageSize = CGSizeZero;
  51. CGSize titleSize = CGSizeZero;
  52. NSDictionary *titleAttributes = nil;
  53. UIImage *backgroundImage = nil;
  54. UIImage *image = nil;
  55. CGFloat imageStartingY = 0.0f;
  56. if ([self isSelected]) {
  57. image = [self selectedImage];
  58. backgroundImage = [self selectedBackgroundImage];
  59. titleAttributes = [self selectedTitleAttributes];
  60. if (!titleAttributes) {
  61. titleAttributes = [self unselectedTitleAttributes];
  62. }
  63. } else {
  64. image = [self unselectedImage];
  65. backgroundImage = [self unselectedBackgroundImage];
  66. titleAttributes = [self unselectedTitleAttributes];
  67. }
  68. imageSize = [image size];
  69. CGContextRef context = UIGraphicsGetCurrentContext();
  70. CGContextSaveGState(context);
  71. [backgroundImage drawInRect:self.bounds];
  72. // Draw image and title
  73. if (![_title length]) {
  74. [image drawInRect:CGRectMake(roundf(frameSize.width / 2 - imageSize.width / 2) +
  75. _imagePositionAdjustment.horizontal,
  76. roundf(frameSize.height / 2 - imageSize.height / 2) +
  77. _imagePositionAdjustment.vertical,
  78. imageSize.width, imageSize.height)];
  79. } else {
  80. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
  81. titleSize = [_title boundingRectWithSize:CGSizeMake(frameSize.width, 20)
  82. options:NSStringDrawingUsesLineFragmentOrigin
  83. attributes:@{NSFontAttributeName: titleAttributes[NSFontAttributeName]}
  84. context:nil].size;
  85. imageStartingY = roundf((frameSize.height - imageSize.height - titleSize.height) / 2);
  86. [image drawInRect:CGRectMake(roundf(frameSize.width / 2 - imageSize.width / 2) +
  87. _imagePositionAdjustment.horizontal,
  88. imageStartingY + _imagePositionAdjustment.vertical,
  89. imageSize.width, imageSize.height)];
  90. CGContextSetFillColorWithColor(context, [titleAttributes[NSForegroundColorAttributeName] CGColor]);
  91. [_title drawInRect:CGRectMake(roundf(frameSize.width / 2 - titleSize.width / 2) +
  92. _titlePositionAdjustment.horizontal,
  93. imageStartingY + imageSize.height + _titlePositionAdjustment.vertical,
  94. titleSize.width, titleSize.height)
  95. withAttributes:titleAttributes];
  96. } else {
  97. #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
  98. titleSize = [_title sizeWithFont:titleAttributes[UITextAttributeFont]
  99. constrainedToSize:CGSizeMake(frameSize.width, 20)];
  100. UIOffset titleShadowOffset = [titleAttributes[UITextAttributeTextShadowOffset] UIOffsetValue];
  101. imageStartingY = roundf((frameSize.height - imageSize.height - titleSize.height) / 2);
  102. [image drawInRect:CGRectMake(roundf(frameSize.width / 2 - imageSize.width / 2) +
  103. _imagePositionAdjustment.horizontal,
  104. imageStartingY + _imagePositionAdjustment.vertical,
  105. imageSize.width, imageSize.height)];
  106. CGContextSetFillColorWithColor(context, [titleAttributes[UITextAttributeTextColor] CGColor]);
  107. UIColor *shadowColor = titleAttributes[UITextAttributeTextShadowColor];
  108. if (shadowColor) {
  109. CGContextSetShadowWithColor(context, CGSizeMake(titleShadowOffset.horizontal, titleShadowOffset.vertical),
  110. 1.0, [shadowColor CGColor]);
  111. }
  112. [_title drawInRect:CGRectMake(roundf(frameSize.width / 2 - titleSize.width / 2) +
  113. _titlePositionAdjustment.horizontal,
  114. imageStartingY + imageSize.height + _titlePositionAdjustment.vertical,
  115. titleSize.width, titleSize.height)
  116. withFont:titleAttributes[UITextAttributeFont]
  117. lineBreakMode:NSLineBreakByTruncatingTail];
  118. #endif
  119. }
  120. }
  121. // Draw badges
  122. if ([[self badgeValue] length]) {
  123. CGSize badgeSize = CGSizeZero;
  124. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
  125. badgeSize = [_badgeValue boundingRectWithSize:CGSizeMake(frameSize.width, 20)
  126. options:NSStringDrawingUsesLineFragmentOrigin
  127. attributes:@{NSFontAttributeName: [self badgeTextFont]}
  128. context:nil].size;
  129. } else {
  130. #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
  131. badgeSize = [_badgeValue sizeWithFont:[self badgeTextFont]
  132. constrainedToSize:CGSizeMake(frameSize.width, 20)];
  133. #endif
  134. }
  135. CGFloat textOffset = 2.0f;
  136. if (badgeSize.width < badgeSize.height) {
  137. badgeSize = CGSizeMake(badgeSize.height, badgeSize.height);
  138. }
  139. CGRect badgeBackgroundFrame = CGRectMake(roundf(frameSize.width / 2 + (image.size.width / 2) * 0.9) +
  140. [self badgePositionAdjustment].horizontal,
  141. textOffset + [self badgePositionAdjustment].vertical,
  142. badgeSize.width + 2 * textOffset, badgeSize.height + 2 * textOffset);
  143. if ([self badgeBackgroundColor]) {
  144. CGContextSetFillColorWithColor(context, [[self badgeBackgroundColor] CGColor]);
  145. CGContextFillEllipseInRect(context, badgeBackgroundFrame);
  146. } else if ([self badgeBackgroundImage]) {
  147. [[self badgeBackgroundImage] drawInRect:badgeBackgroundFrame];
  148. }
  149. CGContextSetFillColorWithColor(context, [[self badgeTextColor] CGColor]);
  150. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
  151. NSMutableParagraphStyle *badgeTextStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
  152. [badgeTextStyle setLineBreakMode:NSLineBreakByWordWrapping];
  153. [badgeTextStyle setAlignment:NSTextAlignmentCenter];
  154. NSDictionary *badgeTextAttributes = @{
  155. NSFontAttributeName: [self badgeTextFont],
  156. NSForegroundColorAttributeName: [self badgeTextColor],
  157. NSParagraphStyleAttributeName: badgeTextStyle,
  158. };
  159. [[self badgeValue] drawInRect:CGRectMake(CGRectGetMinX(badgeBackgroundFrame) + textOffset,
  160. CGRectGetMinY(badgeBackgroundFrame) + textOffset,
  161. badgeSize.width, badgeSize.height)
  162. withAttributes:badgeTextAttributes];
  163. } else {
  164. #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
  165. [[self badgeValue] drawInRect:CGRectMake(CGRectGetMinX(badgeBackgroundFrame) + textOffset,
  166. CGRectGetMinY(badgeBackgroundFrame) + textOffset,
  167. badgeSize.width, badgeSize.height)
  168. withFont:[self badgeTextFont]
  169. lineBreakMode:NSLineBreakByTruncatingTail
  170. alignment:NSTextAlignmentCenter];
  171. #endif
  172. }
  173. }
  174. CGContextRestoreGState(context);
  175. }
  176. #pragma mark - Image configuration
  177. - (UIImage *)finishedSelectedImage {
  178. return [self selectedImage];
  179. }
  180. - (UIImage *)finishedUnselectedImage {
  181. return [self unselectedImage];
  182. }
  183. - (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage {
  184. if (selectedImage && (selectedImage != [self selectedImage])) {
  185. [self setSelectedImage:selectedImage];
  186. }
  187. if (unselectedImage && (unselectedImage != [self unselectedImage])) {
  188. [self setUnselectedImage:unselectedImage];
  189. }
  190. }
  191. - (void)setBadgeValue:(NSString *)badgeValue {
  192. _badgeValue = badgeValue;
  193. [self setNeedsDisplay];
  194. }
  195. #pragma mark - Background configuration
  196. - (UIImage *)backgroundSelectedImage {
  197. return [self selectedBackgroundImage];
  198. }
  199. - (UIImage *)backgroundUnselectedImage {
  200. return [self unselectedBackgroundImage];
  201. }
  202. - (void)setBackgroundSelectedImage:(UIImage *)selectedImage withUnselectedImage:(UIImage *)unselectedImage {
  203. if (selectedImage && (selectedImage != [self selectedBackgroundImage])) {
  204. [self setSelectedBackgroundImage:selectedImage];
  205. }
  206. if (unselectedImage && (unselectedImage != [self unselectedBackgroundImage])) {
  207. [self setUnselectedBackgroundImage:unselectedImage];
  208. }
  209. }
  210. @end

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多