0

我有一个小问题。基本上,我有一个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

4

2 回答 2

2

主要问题是您正在UINavigationBar手动创建一个。你想要的是使用 a 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并将其设置为应用程序中的主视图控制器。你想要的是将 aRootViewController放入 aUINavigationController中,如下所示:

// 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,因为它不再需要了。

于 2013-09-16T19:36:10.273 回答
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
于 2013-09-16T13:36:52.627 回答