I have an application which is bascially a TabBar where the tabs are UINavigationControllers which move back and forth between different ViewControllers containing UIWebViews. My problem is that although I can click on the buttons for the UIWebViews, I can't scroll the content when it is larger than the screen.
Am creating the TabController
tabBarController = [[MainTabBarController alloc] init];
[tabBarController setDelegate:self];
When switching between tabs, I have this code:
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
UIWebView *wv = [[UIWebView alloc] init];
[wv setUserInteractionEnabled:YES];
[wvc setTitle:[nc title]];
[wv setDelegate:self];
[wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]]];
[wvc setWebpage:wv];
[wvc setView:wv];
[nc pushViewController:wvc animated:NO];
NSLog(#"NC.viewcontroller count: %d", [nc.viewControllers count]);
[nc setLastURL:requestURL];
[wvc startLoaderIndicator];
[wv release];
[wvc release];
requestOk = YES;
And when a link within one of the UIWebViews is clicked, I have this:
ItemNavigationController *nc = (ItemNavigationController *)[tabBarController selectedViewController];
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
//wvc.view = wvc.sView;
[wvc setTitle:[nc title]];
[wv setDelegate:self];
[wv loadRequest:request];
[wvc setWebpage:wv];
[wvc setView:wv];
[nc pushViewController:wvc animated:NO];
NSLog(#"NC.viewcontroller count: %d", [nc.viewControllers count]);
[nc setLastURL:request.URL.absoluteString];
[wvc startLoaderIndicator];
[wv release];
[wvc release];
requestOk = YES;
Everything works perfectly, except that the WebViews don't scroll. It looks something like this:
First thing I don't like in this code block...
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
wvc.view = wvc.sView;
Instead create and add the subView to the view in the viewDidLoad block of the view controller. Furthermore, avail yourself of IB and add the WebView via the XIB and simply connect the IBOutlet and delegate.
If you insist upon doing it this way don't overwrite the view,
[wvc.view addSubview:wv];
I wrote this in a simple WebViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://news.google.com"]]];
}
and called it from a AppDelegate like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
CPWebController *webController = [[CPWebController alloc] init];
CPNavController *navController = [[CPNavController alloc] initWithRootViewController:webController];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
It sizes correctly, has a navbar, and scrolls correctly. Please forgive the lack of releases for code brevity.
From what I can see you're setting it with a fixed height here:
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,**480**)];
So the content size is only going to be 480 so there's nowhere to scroll.
Try something like (0,0,320,1000); to test if that is the issue
If so set the height to be the cotent size so it's dynamic
Create WebViewController class and inside .m in viewDidLoad add:
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[wv setDelegate:self];
NSURL*url = [NSURL URLWithString:#"http://www.link.com/twitter_iPhone.php"];
[wv loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:wv];
}
Related
I am implement the TabBar app for ipad & iphone both. But In IPAD the navigation controller is not working for ipad nib in table cell. The viewdid Load method is not call for ipad.
But it work fine for iphone..
self.navigationController is NULL for ipad only in my class to navigate.
Help me!!
my code is as follow:
//Connet.m:
if(i==1)
{
TwitterController *tc;
if ([self isPad]) {
tc = [[TwitterController alloc] initWithNibName:#"TwitterController_ipad" bundle:nil];
}
else
tc = [[TwitterController alloc] initWithNibName:#"TwitterController" bundle:nil];
[self.navigationController pushViewController:tc animated:YES];
NSLog(#"%#",self.navigationController); /Problem ****NULL*****///////
//TWITTER.m
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//Custom initialization
self.navigationController.navigationBarHidden = YES;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
NSString *urlAddress = #"https://twitter.com/UJUDGEIT1";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webv loadRequest:requestObj];
}
Try the following code:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
NSString *nibname=#"";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
nibname=#"TwitterController_ipad";
}
else{
nibname=#"TwitterController";
}
TwitterController *view = [[TwitterController alloc] initWithNibName:nibname bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:view];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Write this code in your app delegate class. The viewDidLoad method gets called.
piyush
how you implement your tab bar.
make each controller of tab bar as navigation controller. it may called your view did load method.
following code may help you. it create 2 controller on tab bar. finally at it to root view controller.
-(void)createTabbar{
NSMutableArray *controllers = [[NSMutableArray alloc] init];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//Controller 1
PasswordVC *pvc = [[PasswordVC alloc] initWithNibName:#"PasswordVC" bundle:nil];
UINavigationController *passwordNav = [[UINavigationController alloc] initWithRootViewController:pvc];
passwordNav.navigationController.navigationBarHidden=YES;
[controllers addObject:passwordNav];
pvc.title = #"Password";
[pvc release];
[passwordNav release];
//Controller 2
SettingVC *SVC = [[SettingVC alloc] initWithNibName:#"SettingVC" bundle:nil];
UINavigationController *settingNav = [[UINavigationController alloc] initWithRootViewController:SVC];
settingNav.navigationController.navigationBarHidden=YES;
[controllers addObject:settingNav];
SVC.title = #"Settings";
[SVC release];
[settingNav release];
}
else { //ipad
}
_tabBarController = [[[UITabBarController alloc] init] autorelease];
_tabBarController.delegate=self;
_tabBarController.viewControllers = controllers;
_tabBarController.customizableViewControllers = controllers;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_window cache:NO];
_window.rootViewController = _tabBarController;
[UIView commitAnimations];
}
I have setup 4 view controllers each containing a UIWebView and set these up as the Tabs for a UITabBarController.
The content loads fine, and the buttons are clickable, however the content of the UIWebViews refuses to scroll. UIWebView has an inbuilt UIScrollView to handle it's own scrolling, so it should just scroll right?
My initialisation code in the main file is:
tabBarController = [[MainTabBarController alloc] init];
[tabBarController.view setBounds:CGRectMake(0, 0, dev_portrait.size.width, 44)];
[tabBarController setDelegate:self];
UIImage* anImage = [UIImage imageNamed:#"53-house.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:#"Home" image:anImage tag:0];
WebViewController *vc1 = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
[vc1 setup];
[vc1 setTabBarItem:theItem];
/// ... same for the other 3 WebViewControllers
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, vc3, vc4, aNavigationController, nil];
tabBarController.viewControllers = controllers;
//self.window.rootViewController = tabBarController;
self.window.rootViewController = tabBarController;
When a tab is touched, it fires a function in the view controller which loads the URL:
- (void)handleURL:(NSString *)url {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSLog(#"Handling URL: %#", request);
[webpage loadRequest:request];
lastURL = url;
[self startLoaderIndicator];
}
I need to create a rich text editor (for text alignment, fonts, bold, italics, underlining etc) for an iPhone app. I have heard of storing the data as HTML and rendering it in a UIWebView. I want to allow the user to edit the data and in my app I am using TapDetectingWindow to detect touches on a UIWebView (exactly as detailed here).
- (id)init {
self = [super init];
if (self) {
tapDetectingWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]; //mWindow, variable reference to TapDetectingWindow
tapDetectingWindow.viewToObserve = webView;
tapDetectingWindow.controllerThatObserves = self;
webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 340, 1900)];
[webView setTag:1];
[webView addSubview:keyboardText];
[webView setDelegate:self];
[webView setOpaque:0.0];
[webView loadHTMLString:htmlString baseURL:NULL];
[self.view addSubview:webView];
keyboardText = [[UITextField alloc] init];
keyboardText.autocorrectionType = UITextAutocorrectionTypeNo;
[keyboardText setDelegate:self];
[self.view addSubview:keyboardText];
}
return self;
}
But my app is crashing, with the message
tapDetectingWindow.viewToObserve = webView
and a report
* -[UIWindow setViewToObserve:]: unrecognized selector sent to instance 0x4a26b90
After a few hours of hair pulling, here's the answer: you need to edit your AppDelegate's .m file to get the TapDetectingWindow to work properly:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController* vc = self.window.rootViewController;
self.window = [[TapDetectingWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = vc;
return YES;
}
I tried this and it's working perfectly with the rest of the TapDetectingWindow tutorial!
My UITabBarController's tabBar is slightly off the view, please can you tell me what is wrong with my code:
LoggedInViewController *lvc = [[[LoggedInViewController alloc]
initWithAccount:account] autorelease];
[self presentModalViewController:lvc animated:YES];
- (void)viewDidLoad
{
self.tabController = [[UITabBarController alloc] init];
LoggedInFeedNavigationController *navController;
navController = [[LoggedInFeedNavigationController alloc]
initWithAccount:self.account];
[self.tabController setViewControllers:
[NSArray arrayWithObject:navController]];
[self.view addSubview:self.tabController.view];
[super viewDidLoad];
}
You are adding the tabController view as a subview, but you have not specified where it should be located within its parent view, or how it should be resized when the parent view changes size. Try the following:
- (void)viewDidLoad
{
[super viewDidLoad]; // see note
self.tabController = [[UITabBarController alloc] init];
LoggedInFeedNavigationController *navController;
navController = [[LoggedInFeedNavigationController alloc]
initWithAccount:self.account];
[self.tabController setViewControllers:
[NSArray arrayWithObject:navController]];
UIView *tabView = self.tabController.view;
[self.view addSubview:tabView];
tabView.frame = self.view.bounds;
tabView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
}
Note: you are not required to call [super viewDidLoad], but if you do decide to call it, you should call it at the beginning of your viewDidLoad method, and not at the end.
Hi all
I have a tabbar based application. In one view I have a TTTabStrip with different TTTabItems. Everything works (loading normal UIViewControllers) except loading UIWebViews.
In the app delegate I have set up the url mapping:
TTNavigator* navigator = [TTNavigator navigator];
navigator.supportsShakeToReload = YES;
navigator.persistenceMode = TTNavigatorPersistenceModeAll;
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]]; // default klasse wenn nichts anderes gewählt ist.
[map from:#"tt://test" toViewController:[TestController class]];
The test controller contains one UIWebView. I can see (NSLog) that the loadView-Method gets called, but the URL is not being opened:
- (void) loadView {
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
self.view = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
//self.view.backgroundColor = [UIColor blackColor];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.com"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
// support zooming
webView.scalesPageToFit = YES;
}
For instance, I can change the background of the UIWebView to black.
How can I load google in the web view below the TTTabStrip?
Thanks a lot.
Try the following and add the method viewWillAppear:
- (void)loadView
{
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
// support zooming
webView.scalesPageToFit = YES;
[newView addSubview:webView];
self.view = newView;
[newView release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.com"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
}