我有一个小问题。
基本上,我有一个位于导航栏下方的UIWebView
,因此我需要向下滚动才能查看内容,并在最后向上滚动。如何让UIWebView
出现在导航栏的正下方?
#import "RootViewController.h"
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
webview=[[UIWebView alloc]initWithFrame:self.view.bounds];
NSString *url=@"http://localhost/";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
navBar = [[UINavigationBar alloc] init];
navBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
UINavigationItem*navItem = [[[UINavigationItem alloc] initWithTitle:@"iPaint"] autorelease];
UIBarButtonItem*leftButton = [[[UIBarButtonItem alloc] initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed)] autorelease];
navItem.leftBarButtonItem = leftButton;
UIBarButtonItem*rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed)] autorelease];
navItem.rightBarButtonItem = rightButton;
navBar.barStyle = UIBarStyleDefault;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
-(void)leftButtonPressed{
//Code Here
}
-(void)rightButtonPressed{
//Code Here
}
@end
main.mm代码:
int main(int argc, char **argv) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
int ret = UIApplicationMain(argc, argv, @"comvlad1kwebApplication", @"comvlad1kwebApplication");
[p drain];
return ret;
}
// vim:ft=objc
我正在使用Theos,所以我没有Xcode,因此我需要手动执行。 谢谢。
编辑:以下是屏幕截图的链接:http://i.imgur.com/aQ4yuyp.png
答案 0 :(得分:2)
主要问题是您手动创建UINavigationBar
。你想要的是使用UINavigationController
,它是一个视图控制器,管理一堆其他视图控制器和它们之间的导航。在您的应用程序委托中,您可能具有如下所示的代码:
// Assuming that your UIApplication Delegate has a _viewController ivar which is properly memory-managed.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_viewController = [[RootViewController alloc] init];
[_window addSubview:_viewController.view];
[_window makeKeyAndVisible];
}
该代码片段创建RootViewController
的实例,并将其设置为应用程序中的主视图控制器。您想要的是将RootViewController
放在UINavigationController
内,如下所示:
// Assuming that your UIApplication Delegate has a _viewController ivar which is properly memory-managed.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_viewController = [[RootViewController alloc] init];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:_viewController] autorelease];
[_window addSubview:navController.view];
[_window makeKeyAndVisible];
}
创建UINavigationController
子类后,您应该从UINavigationBar
中删除RootViewController
,因为不再需要它。
答案 1 :(得分:0)
您不应根据viewDidLoad方法中控制器的视图框架设置视图框架。在viewWillAppear上设置webView和NavigationBar的框架。
此外,您可能希望使用UINAvigationController,而不是手动添加UINavigationBar
#import "RootViewController.h"
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
webview=[[UIWebView alloc]initWithFrame:CGrectZero];
NSString *url=@"http://localhost/";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
navBar = [[UINavigationBar alloc] init];
UINavigationItem*navItem = [[[UINavigationItem alloc] initWithTitle:@"iPaint"] autorelease];
UIBarButtonItem*leftButton = [[[UIBarButtonItem alloc] initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed)] autorelease];
navItem.leftBarButtonItem = leftButton;
UIBarButtonItem*rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed)] autorelease];
navItem.rightBarButtonItem = rightButton;
navBar.barStyle = UIBarStyleDefault;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
- (void)viewWillAppear:(BOOL)animated{
navBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
CGRect r = self.view.bounds;
r.origin.y = CGRectGetMaxY(navBar.frame);
r.size.width = r.size.width - CGRectGetMaxY(navBar.frame);
[webView setFrame:r];
}
-(void)leftButtonPressed{
//Code Here
}
-(void)rightButtonPressed{
//Code Here
}
@end