我有一个小问题。
基本上,我有一个位于导航栏下方的 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
最佳答案
主要问题是您正在手动创建 UINavigationBar
。您想要的是使用 UINavigationController
,它是一个 View Controller ,用于管理其他 View Controller 的堆栈以及它们之间的导航。在您的应用程序委托(delegate)中,您可能拥有如下所示的代码:
// 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
实例,并将其设置为应用程序中的主视图 Controller 。您想要的是将 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
子类,您应该从您的 RootViewController
中删除 UINavigationBar
,因为它不再是必需的。
关于ios - 为什么导航栏在 UIViewWeb 之上? (OBJ-C 初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18828919/