uiwebview zoom programmatically - uiwebview

I want to change the UIWebView zoom level programmatically.
I have tried these but they are not worked for me ;
UIScrollView *sv = [theWebView.subviews objectAtIndex:0];
[sv setZoomScale:50 animated:YES];
also,
[theWebView stringByEvaluatingJavaScriptFromString:#"document.body.style.zoom = 5.0;"];
Any idea?

This is the only way I could find to accomplish it:
(specify your own sizes if you wish, i was attempting to zoom out after typing into a form field)
UIScrollView *sv = [[webViewView subviews] objectAtIndex:0];
[sv zoomToRect:CGRectMake(0, 0, sv.contentSize.width, sv.contentSize.height) animated:YES];

I would use:
UIScrollView * sv = self.theWebview.scrollView;
[sv setZoomScale:50];
where self is the viewController containing the webView.
Your problem could be the size of your zoomScale exceeds the size of sv.maximumZoomScale.

UIScrollView * sv = yourwebview_outlet.scrollView;
[sv setZoomScale:50];

Related

Why do navigation appear 20 pixels below status bar in the view? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UIWebView on iPad size
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *naviController = [[UINavigationController alloc]init];
[self.view addSubview:naviController.view];
}
If I add navigation controller in the view, it appears about 20 pixels below status bar.
I want it appears just below status bar. How do I fix this?
I've experienced this same issue.
This discussion was helpful: UIWebView on iPad size
Tony's answer helped me discover this trick:
naviController.view.frame = self.view.bounds;
Turns out setting the view height fixes this problem. You can do it in the nib (xib) file or progammatically when creating the view setting the view.frame = CGRect(0, 0, 320.0 460.0); or whatever it's supposed to be (in your case it looks like it should be 460.0 since you have the status bar on).
The main point is that if the view size is smaller than the height of the screen, there are no guarantees where your controls will show up.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect frame = CGRectMake(0, 0, 320.0, 480.0);
viewController.view.frame = frame;
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
I changed like above. It works. So far so good. If height was 460, there would have been blank line at the bottom. View's absolute coordinate is (0, 20). So, Navigation bar's coordinate is (0, 40). I think that was the problem. I will encounter another problem.
Maybe there is way to change coordinate before displaying navigation view. Thank you.

how can I set contentoffset and change the height of UIScrollview at same time

I'm trying to scroll up my UISCrollView when the keyboard is shown
I use setContentOffset to shift the uiview up.
At the same time I want to decrease the height of my UISCrollView to (view height - keyboard height), so that the entire content view can be scrolled.
I apply both the changes in keyboardWillShow notification
When I actually bring the keyboard up in my app, the contents first get pushed up and then they are pushed down (which gives a flickering effect). I'm trying to smooth out both the transformations in one go..
Is it possible?
Code Below ---
- (void) keyboardWillShow {
CGPoint contentOffset = scrollView.contentOffset;
CGRect scrollViewFrame = scrollView.frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
if (contentOffset.y >= 0 && contentOffset.x >= 0) {
isContentOffset = YES;
contentOffset.y += screenShift;
[scrollView setContentOffset:contentOffset animated: NO];
}
scrollViewFrame.size.height = keyboardOrigin.y - self.view.frame.origin.y - toolbarHeight;
scrollView.frame = scrollViewFrame;
[UIView commitAnimations];
}
There is an option for animation when you setContentOffset for animation. here is the code that i use all the time
- (void)textViewDidBeginEditing:(UITextView *)textView
{
svos = scrollView.contentOffset;
CGRect rect = [textView bounds];
rect = [textView convertRect:rect toView:self.scrollView];
CGPoint point = rect.origin ;
point.x = 0 ;
[self.scrollView setContentOffset:point animated:YES];
doneButton.enabled = YES;
}
- (IBAction)donePressed
{
[scrollView setContentOffset:svos animated:YES];
[textview resignFirstResponder];
doneButton.enabled = NO;
}
It works fine for me.
Are you wrapping these changes in an animation block?
[UIView beginAnimations:#"resizeScroll" context:nil];
// make your changes to set and content offset
[UIView commitAnimations];
I think I have got a solution for this. You can handle the resize of the view in textViewDidBeginEditing method. You can just change the frame size of the scrollView to half by
CGRect tframe = myscrollView.frame;
tframe.size.height/=2;
myscrollView.frame = tframe;
and to initialize or handle the total length of the scrollview you can set the contentSize of the scrollView in the similar fashion as the frame was set in above snippet. for example
CGSize tsize = self.view.frame.size;
//here you can change the height and width of the scrollView
myscrollView.contentSize = tsize;
I hope this should do the trick for you. If it does please communicate.
Figured out the issue...
When the keyboard button was clicked, I was doing a becomeFirstResponder followed by a resignFirstResponder on keyboard view. This caused keyboardWillShow followed by keyboardWillHide and another keyboardWillShow notification, which brought the keyboard up, brought it back down and then up again.
Thanks to everybody who tried to help out.. and sorry the issue was pretty different...

problem: menu does not show in cocos2d layer

I am trying to add menu to a layer in cocos2d but it just does not appear. Here's the code which is written in init method of a layer
CCMenuItem *aButton = [CCMenuItemImage itemFromNormalImage:#"btnImg.png" selectedImage:#"btnImgSel.png" target:self selector:#selector(buttonPressed:)];
aButton.position = ccp(60.0,30.0);
CCMenu *aMenu = [CCMenu menuWithItems:aButton, nil];
aMenu.position = ccp(500.0,20);
[self addChild:aMenu];
Nothing is overlapping the position i specified for menu. Is anything wrong in the code?
Try like these:-
CCLayer *menuLayer1 = [[[CCLayer alloc] init]autorelease];
[self addChild:menuLayer1];
CCMenuItemImage *startButton1 = [CCMenuItemImage
itemFromNormalImage:#"Play.png"
selectedImage:#"Play.png"
target:self
selector:#selector(Play:)];
CCMenu *menu1 = [CCMenu menuWithItems: startButton1,nil];
menu1.position = ccp(157,157 );
[menu1 alignItemsVertically ];
[menuLayer1 addChild: menu1];
For those who are facing an irritating situation where the code is right but menu items are not showing then check the image file. I was using .png images and they were refusing to be displayed. There was something internally wrong with the file, so I replaced that file and it solved the problem :)
Is the iPad your target platform? If so the "menu" should appear at the bottom of the screen. To display the Menu on iPhone adjust the "a.Menu.position" to anything lower than 480 in the first attribute of ccp

Second View shifts upward when flipped from First View

I am building an ap in which I need to remove the present view from the superView and load another one. But the new loaded view shifts upward. Also when the firstView is again loaded, it loads perfectly.
Could anyone possible suggest something to get rid of this issue.
Following is the method I am using to flip the view to secondView
-(void) flipToBack
{
CreateMessageViewController *oSecondView = [[CreateMessageViewController alloc] initWithNibName:#"CreateMessageViewController" bundle:nil];
[self setMsgViewController:oSecondView];
[oSecondView release];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:window cache:YES];
[viewController.view removeFromSuperview];
[tabBarController.view removeFromSuperview];
[self.window addSubview:[msgViewController view]];
[UIView commitAnimations];
}
Thanks in advance!!
Looking forward to your kind responses.
thanks
There is not much I can tell without looking at the big picture. Check the frames in the IB to see if they are correctly set.
Is this code from your appDelegate? Does viewController have a toolbar shown on his view? Maybe your view is shifted the exact pixels your toolbar height is?
Got it.
It's happening due to the IB settings. So in IB your viewController has a statusBar and your view has a height of 460 (for iPhone) not 480. The 20 points are the statusBar height. When transitioning to the second view, its rect is {0,0,320,460} so the view is positioned behind the application's statusBar and you are missing 20 points from the bottom.
To get around this you need to:
Go into IB and set the CreateMessageViewController's statusBar to Unspecified.
Go into the view size section and set the view's height to 480 (for iPhone).
Now your CreateMessageViewController's view will show properly.
After long time playing around with the code and IB, I found the bug. It was corrected as
Open the mainWindow.xib and add the UIViewController Object from the Library. Change its class to your Class that handles the specified viewController and then comes the click. Go to the properties section of the Attribute Inspector and set the respective Nib Name for the Controller and now save and exit the IB.
And the code changes to
-(void) flipToBack
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:window cache:YES];
[viewController.view removeFromSuperview];
[tabBarController.view removeFromSuperview];
[self.window addSubview:[msgViewController view]];
[UIView commitAnimations];
}
Thanks to all who responded!!

Changing UIWebView default color to UIImage

Is it possible to place an image in the UIWebView background instead of that gray default color. If yes then how?
Did it by myself.
self.webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:backgroundImage]];
In your viewDidLoad please Add this code for loading image at startup
[webView setOpaque:NO];
webView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"night.jpg"]];
self.webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:imagePath]];

Resources