2013-09-16 31 views
0

我有一個小問題。 基本上,我有一個位於導航欄下的UIWebView,所以我需要向下滾動查看內容,並在最後滾動。我如何讓UIWebView顯示在導航欄的正下方?爲什麼導航欄位於UIViewWeb之上? (初學者OBJ-C)

#import "RootViewController.h" 
@implementation RootViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    webview=[[UIWebView alloc]initWithFrame:self.view.bounds]; 
    NSString *[email protected]"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 

我使用西奧斯,所以我沒有一個Xcode,所以我需要做手工。 謝謝。

編輯:這裏是一個鏈接到一個截圖:http://i.imgur.com/aQ4yuyp.png

回答

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子類,你應該從你RootViewController刪除UINavigationBar,因爲它不再是必要的。

0

你不應該設置的意見框架,基於控制器的觀點,在viewDidLoad方法框架。在viewWillAppear上設置webView和NavigationBar的框架。

而且,很可能你會想使用的UINavigationController,而不是添加UINavigationBar的手動

#import "RootViewController.h" 
@implementation RootViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    webview=[[UIWebView alloc]initWithFrame:CGrectZero]; 
    NSString *[email protected]"http://localhost/"; 
    NSURL *nsurl=[NSURL URLWithString:url]; 
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl]; 
    [webview loadRequest:nsrequest]; 
    [self.view addSubview:webview]; 

導航欄= [[UINavigationBar的頁頭] 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 
+0

你能編輯我的代碼並修復它嗎?我剛開始使用ObjC,並且我不太滿意它。謝謝。 – VladHQ

+0

在這裏,你去。我希望一切正常 –

+0

我沒有AppDelegate,我只有RootViewController.h,RootViewController.mm和main.mm.? – VladHQ