ios - 为什么导航栏在 UIViewWeb 之上? (OBJ-C 初学者)

标签 ios objective-c uiwebview theos

我有一个小问题。 基本上,我有一个位于导航栏下方的 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/

相关文章:

iphone - XCOde 4 如何在 xib 中旋转 View ?

ios - 无法在强制转换中将类型 'ReverseRandomAccessCollection<[AnyObject]>’ 的值转换为类型 '[PFObject]'

ios - swift 地,我如何以编程方式制作 View 层?

objective-c - 使用 AFNetworking 通过 HTTP 身份验证获取 JSON

objective-c - 像Cloud App一样在Mac上自动上传截图

ios - WKWebView 上的 AngularJS : any solution for handling file://on iOS 9?

uitableview - 确定 UIWebView 内容的高度(嵌入 UITableViewCell 中)

ios - 在代码中使用自动布局调整 UIButtons 大小的最佳实践?

objective-c - iOS 5 : Text Color for UIBarButtonSystemItemAdd

xcode - 如何在 Swift 中以编程方式添加约束