iOS Objective-C新手求助:实现显示今日及后6天的自定义Tab栏
嘿,作为Objective-C新手,实现这种显示一周日期的自定义Tab栏其实没那么复杂,我给你梳理了一步步的实现指引,跟着来就行~
自定义日期Tab栏实现指引
1. 先做一个自定义Tab栏视图(UIView子类)
首先我们要创建一个DateTabBarView,专门用来承载7个日期按钮,处理日期生成和点击事件。这样代码更模块化,后续也好维护。
#import <UIKit/UIKit.h> @interface DateTabBarView : UIView // 定义一个回调block,用来把选中的日期传递给上层页面 @property (nonatomic, copy) void (^dateTapHandler)(NSDate *selectedDate); @end @implementation DateTabBarView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setupAllDateButtons]; } return self; } - (void)setupAllDateButtons { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *today = [NSDate date]; // 循环生成今日+未来6天,共7个日期按钮 for (NSInteger i = 0; i < 7; i++) { NSDate *targetDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:today options:0]; // 格式化按钮标题:今天显示“今天”,其他显示“月/日” NSString *btnTitle; if (i == 0) { btnTitle = @"今天"; } else { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"MM/dd"; btnTitle = [formatter stringFromDate:targetDate]; } // 创建按钮并设置属性 UIButton *dateBtn = [UIButton buttonWithType:UIButtonTypeSystem]; dateBtn.tag = i; // 用tag标记是第几天,方便后续计算日期 [dateBtn setTitle:btnTitle forState:UIControlStateNormal]; [dateBtn addTarget:self action:@selector(onDateButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; // 布局按钮:简单用Frame布局,新手好理解,后续可以换成AutoLayout CGFloat btnWidth = self.frame.size.width / 7; dateBtn.frame = CGRectMake(i * btnWidth, 0, btnWidth, self.frame.size.height); [self addSubview:dateBtn]; } } - (void)onDateButtonTapped:(UIButton *)sender { // 根据tag计算选中的日期 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *selectedDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:sender.tag toDate:[NSDate date] options:0]; // 通过回调把日期传给上层页面处理 if (self.dateTapHandler) { self.dateTapHandler(selectedDate); } } @end
2. 在ViewController中使用自定义Tab栏
接下来把这个Tab栏加到你的页面里,处理点击后的URL跳转逻辑:
#import "DateTabBarView.h" #import <SafariServices/SafariServices.h> // 别忘了导入这个头文件 @interface YourViewController () @end @implementation YourViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建自定义Tab栏,放在页面底部(高度设为50,你可以根据需求调整) CGFloat tabBarHeight = 50; DateTabBarView *dateTabBar = [[DateTabBarView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - tabBarHeight, self.view.frame.size.width, tabBarHeight)]; dateTabBar.backgroundColor = [UIColor whiteColor]; // 设置背景色,避免和页面内容混在一起 // 处理日期点击事件:这里要弱引用self,避免循环引用 __weak typeof(self) weakSelf = self; dateTabBar.dateTapHandler = ^(NSDate *selectedDate) { [weakSelf handleDateSelection:selectedDate]; }; [self.view addSubview:dateTabBar]; } - (void)handleDateSelection:(NSDate *)selectedDate { // 把日期格式化成URL需要的格式,比如yyyy-MM-dd NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd"; NSString *dateString = [formatter stringFromDate:selectedDate]; // 拼接你的指定URL,这里替换成你实际要用的URL模板 NSString *urlString = [NSString stringWithFormat:@"https://your-domain.com/your-path?date=%@", dateString]; NSURL *targetURL = [NSURL URLWithString:urlString]; // 跳转URL:推荐用SFSafariViewController(iOS9+),体验比WebView好 if ([SFSafariViewController class]) { SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:targetURL]; [self presentViewController:safariVC animated:YES completion:nil]; } else { // 兼容iOS8及以下的情况(现在基本很少用到了) UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; [webView loadRequest:[NSURLRequest requestWithURL:targetURL]]; [self.view addSubview:webView]; } } @end
3. 可选优化细节
- 选中状态样式:可以给点击的按钮加个高亮样式,比如改变文字颜色、加边框,让用户清楚当前选中的日期。在
onDateButtonTapped:方法里遍历所有按钮,重置样式后再设置当前按钮的选中样式就行。 - AutoLayout适配:如果要支持屏幕旋转或者不同尺寸的设备,把Frame布局换成AutoLayout会更靠谱。比如给每个按钮添加等宽约束,给Tab栏添加底部、左右的约束。
- 性能优化:把NSDateFormatter提取成全局的,避免每次创建,提升性能。
比如添加选中样式的代码片段:
- (void)onDateButtonTapped:(UIButton *)sender { // 重置所有按钮的样式 for (UIView *subview in self.subviews) { if ([subview isKindOfClass:[UIButton class]]) { UIButton *btn = (UIButton *)subview; [btn setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal]; btn.layer.borderWidth = 0; } } // 设置当前按钮的选中样式 [sender setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal]; sender.layer.borderWidth = 1; sender.layer.borderColor = [UIColor systemBlueColor].CGColor; // 原来的日期处理逻辑... }
内容的提问来源于stack exchange,提问作者Hanz Cheah
相关产品推荐
相关产品推荐

