如何在我的应用程序中添加这样的标题并接收触摸事件并控制它以隐藏/显示?
答案 0 :(得分:3)
这是可以解决的问题。
我为iOS 7制作了一个新项目,只有一个ViewController。这是故事板:
我在顶部添加了一个简单的UIView
,在其下方添加了UINavigationBar
,并在剩余空间中添加了一个大UIView
。最后一个视图是您放置内容的地方。
现在这里是ViewController代码:
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIView *littleView;
@property (weak, nonatomic) IBOutlet UINavigationBar *navBar;
@property (weak, nonatomic) IBOutlet UIView *mainView;
- (IBAction)toggleLittleView:(id)sender;
@end
littleView
是状态栏后面的视图,navbar
是导航栏,mainView
是内容视图,toggleLittleview
链接到导航按钮杆
ViewController.m:
@implementation ViewController {
BOOL isVisible;
CGFloat statusBarOffset;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Setting initial values
isVisible = YES;
statusBarOffset = 20;
// Adding a gesture recognizer to littleView
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[_littleView addGestureRecognizer:tap];
}
// Linked to the nav bar button
- (IBAction)toggleLittleView:(id)sender {
// If littleView is not on screen, show it before animation
if (!isVisible) {
_littleView.hidden = !_littleView.hidden;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
// Animate to the new frames
[UIView animateWithDuration:0.25
animations:^{
_littleView.frame = CGRectOffset(_littleView.frame, 0, isVisible ? -(_littleView.frame.size.height-statusBarOffset) : (_littleView.frame.size.height-statusBarOffset));
_navBar.frame = CGRectMake(_navBar.frame.origin.x, _littleView.frame.origin.y + _littleView.frame.size.height, _navBar.frame.size.width, _navBar.frame.size.height);
CGFloat offSet = isVisible ? self.view.frame.size.height - _navBar.frame.size.height + statusBarOffset : self.view.frame.size.height - _navBar.frame.size.height - _littleView.frame.size.height + statusBarOffset;
_mainView.frame = CGRectMake(_mainView.frame.origin.x, _navBar.frame.origin.y + _navBar.frame.size.height, _mainView.frame.size.width, offSet);
isVisible = !isVisible;
}
completion:^(BOOL finished) {
// If view is not visible after animation, hide it
if (!isVisible) {
_littleView.hidden = !_littleView.hidden;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault];
}
}];
}
// Do stuff on tap
-(void)handleTap:(UIGestureRecognizer*)tap {
[[[UIAlertView alloc] initWithTitle:@"Test" message:@"You tapped !" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
@end
警告强>
此代码并不完美,但它基本上复制了您正在寻找的效果。你应该努力让它按照你的意愿行事。此外,这是通过嵌入UIViewController
的简单UINavigationController
,非实现的,我手动添加了导航栏。此代码无法在嵌入UITableViewController
的{{1}}或UIViewController
中使用。它不使用autolayout,你绝对应该在iOS 6/7下使用。
这是gif预览: