自定义tabbar 怎么适配iphonex tabbar高度

iphone6 tabbar适配的图片大小? - 知乎2被浏览2051分享邀请回答0添加评论分享收藏感谢收起技术文档(8)
首先关于适配iPhoneX,适配安全区讲,因为iPhoneX的刘海和底部上滑指示条的存在,所以弄出来一个安全区的概念。这个区域,就是保证我们的内容的在任何时候,尤其是横屏状态下,内容不被遮盖!而且这个安全区是可以自己设置更改的,也就是,我们可以做成内容占满整个屏幕,当然你创意足够好的话~ Ps:我觉得底部的安全距离,在没有tabbar的页面,去掉还是蛮爽的一件事情!
再说一下,依照我的理解,这个安全区域,白话来讲,其实就是系统给你的一个指导的显示区域的值!参考这个值适配会非常简单。 也就是说,你完全可以不管这个,手动随便设置内容显示到哪里,当然你足够勇敢的话。
我觉得安全区域目前最终的两个属性和一个方法是:
safeAreaInsets和safeAreaLayoutGuide
-(void)viewSafeAreaInsetsDidChange
关于这两个属性和一个方法,网上已经有很多说明,下边是我适配过程中的几个宏,还在适配,还在完善:
// UIScreen width.
#define &LL_ScreenWidth & [UIScreen mainScreen].bounds.size.width
// UIScreen height.
#define &LL_ScreenHeight &[UIScreen mainScreen].bounds.size.height
// iPhone X&
#define &LL_iPhoneX (LL_ScreenWidth == 375.f && LL_ScreenHeight == 812.f ? YES : NO)
// Status bar height.
#define &LL_StatusBarHeight & & &(LL_iPhoneX ? 44.f : 20.f)
// Navigation bar height.
#define &LL_NavigationBarHeight &44.f
// Tabbar height.
#define &LL_TabbarHeight & & & & (LL_iPhoneX ? (49.f+34.f) : 49.f)
// Tabbar safe bottom margin.
#define &LL_TabbarSafeBottomMargin & & & & (LL_iPhoneX ? 34.f : 0.f)
// Status bar & navigation bar height.
#define &LL_StatusBarAndNavigationBarHeight &(LL_iPhoneX ? 88.f : 64.f)
#define LL_ViewSafeAreInsets(view) ({UIEdgeI if(@available(iOS 11.0, *)) {insets = view.safeAreaI} else {insets =
UIEdgeInsetsZ}})
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2492次
排名:千里之外
原创:26篇
(20)(12)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'关注社区微信公众号: PMvideo
iOS自定义 中间凸出的 tabbar 仿闲鱼
iOS封装好一个自定义控件是一个Developer的基本技能,这个tabbar已经跟了我3个项目了,虽然很简单,也是很费时间的,相比code4App上很多控件来说还是好很多的吧
我会分享下设计这个tabbar的心得,高手就当乐呵了,如有不足非常欢迎你的指出
先来定义tabbar控制器,继承系统的tabbarController来获得...等系统提供的技能(不要和UiKit对着干吧,该继承,还是继承省事)
@interface CYTabBarController : UITabBarController
/** 自定义的tabbar */
@property (strong , nonatomic) CustomTabBar*
* 添加子控制器
* @param Controller
需管理的子控制器
* @param title
* @param imageName
未选中的图片名
* @param selectedImageName
选中的图片名
- (void)addChildController:(id)Controller
title:(NSString *)title
imageName:(NSString *)imageName
selectedImageName:(NSString *)selectedImageN
* 设置中间按钮
* @param Controller
需管理的子控制器
* @param title
* @param imageName
未选中的图片名
* @param selectedImageName
选中的图片名
- (void)addCenterController:(id)Controller
bulge:(BOOL)bulge
title:(NSString *)title
imageName:(NSString *)imageName
selectedImageName:(NSString *)selectedImageN
再看tabbarController的m文件,其实我在思考过要不要用链式调用或者其他很酷的方法来完成所有的控制器的添加,但整个m文件也不过100多行挺好的,采用item记录,不浪费系统给每个控制器创建好的属性,后面刚好用来监听提醒数字的改变
@interface CYTabBarController ()
/**标示中间按钮位置*/
@property(assign , nonatomic) NSInteger centerP
/** 标示中间按钮是否突出 */
@property(assign , nonatomic,getter=is_bulge) BOOL
/** 添加所有刚加进来的item ,方便后面打包传给tabbar来生成按钮 */
@property (nonatomic,strong) NSMutableArray &UITabBarItem *&*
我的思路:对于中间按钮,我在设计的时候不想它非要按照顺序添加到头或者尾
所以需要个属性来记录位置,刚好又能承担是否有中间按钮的角色。
而且对于按钮是否凸出也需要记录,这里似乎没有什么更好的办法。
对于这个item数组,我也在思考要不要,我没见过超过5个控制器的应用,占用不了多少内存,元素也只是对控制器的item的一个强引用,所以我加上了
这样做的好处是,在这个控制器里该做的事,做好了再做为模型一样整体给tabbar去显示
添加其他控制器,并收集item
- (void)addChildController:(id)Controller title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
UIViewController *vc = [self findViewControllerWithobject:Controller];
vc.tabBarItem.title =
vc.tabBarItem.image = [UIImage imageNamed:imageName];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
vc.tabBarItem.tag = tabBarItemTag++;
[self.items addObject:vc.tabBarItem];
[self addChildViewController:Controller];
添加中间按钮,并收集item
- (void)addCenterController:(id)Controller bulge:(BOOL)bulge title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
if (Controller) {
[self addChildController:Controller title:title imageName:imageName selectedImageName:selectedImageName];
self.centerPlace = tabBarItemTag-1;
UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:title
image:[UIImage imageNamed:imageName]
selectedImage:[UIImage imageNamed:selectedImageName]];
item.tag = -1;
[self.items addObject:item];
self.centerPlace = tabBarItemT
对于这里适用KVC传递bulge、controller....
其实并不好,如果你改了编译器并不会告诉你
但是我还是这么做了,我觉得这些生成依赖的属性暴露出来会扰乱其他使用者
- (CustomTabBar *)tabbar{
if (!_tabbar) {
_tabbar = [[CustomTabBar alloc]initWithFrame:self.tabBar.frame];
[_tabbar setValue:[NSNumber numberWithBool:self.bulge] forKey:@"bulge"];
[_tabbar setValue:self forKey:@"controller"];
[_tabbar setValue:[NSNumber numberWithInteger:self.centerPlace] forKey:@"centerPlace"];
_tabbar.items = self.
//remove tabBar
for (UIView *loop in self.tabBar.subviews) {
[loop removeFromSuperview];
self.tabBar.hidden = YES;
[self.tabBar removeFromSuperview];
设置nav的selectedIndex的时候来切换控制器
- (void)setSelectedIndex:(NSUInteger)selectedIndex{
if (selectedIndex &= self.viewControllers.count){
@throw [NSException exceptionWithName:@"selectedTabbarError"
reason:@"Don't have the controller can be used, index beyond the viewControllers."
userInfo:nil];
[super setSelectedIndex:selectedIndex];
UIViewController *viewController = [self findViewControllerWithobject:self.viewControllers[selectedIndex]];
[self.tabbar removeFromSuperview];
[viewController.view addSubview:self.tabbar];
这里不一定是viewController,所以需要抽出来
- (UIViewController *)findViewControllerWithobject:(id)object{
while ([object isKindOfClass:[UITabBarController class]] || [object isKindOfClass:[UINavigationController class]]){
object = ((UITabBarController *)object).viewControllers.firstO
tabBar的h文件
我的思路:后置声明按钮和中间按钮,屏蔽按钮的细节,因为我并不想外界去修改item等
我想过把按钮搞成readonly,但是考虑到自定义性也没这么做
#import &UIKit/UIKit.h&
@class CYB
@class CYCenterB
@interface CustomTabBar : UIView
/** tabbar按钮显示信息 */
@property(copy, nonatomic) NSArray&UITabBarItem *& *
/** 设置文字颜色 */
@property (strong , nonatomic) UIColor *textC
/** 设置选中颜色 */
@property (strong , nonatomic) UIColor *selectedTextC
/** 其他按钮 */
@property (strong , nonatomic) NSMutableArray &CYButton*&*btnA
/** 中间按钮 */
@property (strong , nonatomic) CYCenterButton *centerB
tabBar的m文件
tabbar主要实现就是切换下按钮高亮等Ui操作,不再赘述
因为凸出的tabbar部分不在当前tabbar的view上了所以我们需要拿到当前点击的点
如果是凸出的按钮就让按钮响应这个事件 不是的话就让它继续递归找到响应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGRect rect = self.centerBtn.
if (CGRectContainsPoint(rect, point))
return self.centerB
return [super hitTest:point withEvent:event];
对于点击+号按钮动动画可能你的工程并不需要
你只需要在中间按钮点击的方法中移除就好了,这个方法在中间按钮是控制器时并不会生效
- (void)centerBtnClick:(CYCenterButton *)button{
[PlusAnimate standardPublishAnimateWithView:button];
对于按钮 和 中间按钮只是些布局,可以直接看代码,我已经托管到了git上:
0条评论或问题
笔记社区是一个面向中高端IT开发者、程序员的知识共享社区,通过网络抓取与文章分类总结,由专家为用户提供高质量的专题文章系列。
原文链接:/p/725faff65f14
声明:所有文章资源均从网络抓取,如果侵犯到您的著作权,请联系删除文章。联系方式请关注微信公众号PMvideo【锤子视频-程序员喜欢的短视频】,笔记社区开发者交流群 。
今日签到2人
关注微信公众号:PMvideo

我要回帖

更多关于 ios iphonex tabbar 的文章

 

随机推荐