UIView with rotation issues - objective-c

I've a sample project, where I'm creating an custom UIViewController.
#import "ViewController.h"
#implementation ViewController
#synthesize webViews = _webViews;
#synthesize webView = _webView;
- (void)setWebView:(UIWebView *)webView {
if (webView!=_webView) {
[self.webView removeFromSuperview];
_webView = nil;
_webView = webView;
[self.view addSubview:self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com/"]]];
}
}
- (IBAction)newWebView:(id)sender {
self.webView = [self.webViews objectAtIndex:1];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIWebView *webView1 = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIWebView *webView2 = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.webViews = [NSArray arrayWithObjects: webView1, webView2, nil];
self.webView = [self.webViews objectAtIndex:0];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
Two UIWebView's are created inside the viewWillAppear:animated and stored in an array -- and than the first UIWebView is added to the subview of self.view. When a button in the navigation bar is pressed, the first UIWebView will be removed from the subview and the next one will be added instead.
The problem is, that if I'm running the application in landscape (both iPhone and iPad) after the second UIWebView is added, the WebView is not filling the entire screen. Why?
You can download the small sample project here: http://uploads.demaweb.dk/WebView.zip.

I dont know why but this inside your action method will do the trick...
UIWebView *newWebView=[[UIWebView alloc] init];
newWebView=[self.webViews objectAtIndex:1] ;
newWebView.frame=self.view.frame;
self.webView = newWebView;
hope this helps.. :/

Related

iOS 7 Tab bar icons temporarily disappear when on More tab

When I add a view controller embedded by navigation controller, to a tab bar, its icon + title disappear briefly when coming back to the More tab.
However when the view controller is added as such, the icon+image are okay and don't disappear.
I've tried many things already, and am out of options. Any ideas?
Here's my AppDelegate code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.tabBarController = [[UITabBarController alloc] init];
// Must be placed here, just before tabs are added. Otherwise navigation bar
// will overlap with status bar.
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
[self addViewControllersToTabBar];
self.window.rootViewController = self.tabBarController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)addViewControllersToTabBar
{
NSArray* tabBarClassNames =
#[
NSStringFromClass([FirstViewController class]),
NSStringFromClass([SecondViewController class]),
NSStringFromClass([FirstViewController class]),
NSStringFromClass([FirstViewController class]),
NSStringFromClass([FirstViewController class]),
NSStringFromClass([SecondViewController class]),
NSStringFromClass([FirstViewController class]),
];
NSMutableArray* viewControllers = [NSMutableArray array];
for (NSString* className in tabBarClassNames)
{
UIViewController* viewController = [[NSClassFromString(className) alloc] init];
UINavigationController* navigationController;
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[viewControllers addObject:navigationController];
}
[viewControllers addObject:[[FirstViewController alloc] init]]; // This one is fine.
self.tabBarController.viewControllers = viewControllers;
self.tabBarController.selectedViewController = viewControllers[2];
}
and the view controllers are literally nothing more than:
#implementation SecondViewController
- (instancetype)init
{
if (self = [super init])
{
self.title = #"second";
self.tabBarItem.image = [UIImage imageNamed:#"second.png"];
}
return self;
}
#end
Holy shit, spent so much time on this bug, but here's my workaround. Instead of:
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
I created my own NavigationController as subclass of UINavigationController:
#implementation NavigationController
- (instancetype)initWithRootViewController:(UIViewController*)rootViewController
{
if (self = [super initWithRootViewController:rootViewController])
{
NSString* className = NSStringFromClass([rootViewController class]);
NSString* name = [className stringByReplacingOccurrencesOfString:#"ViewController" withString:#""];
self.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#Tab.png", name]];
}
return self;
}
#end
and then do:
navigationController = [[NavigationController alloc] initWithRootViewController:viewController];
Prerequisite is that tab images have the same base name as the view controller class name, which was already the case in my app.
I was setting self.tabBarItem.image inside the view controllers' init method, and this seems to cause the effect I was seeing. So in addition to using my own navigation controller, I also simply deleted setting the tabBarItem in each individual view controller.

Html form in UIwebview send back to ios?

i am trying to pass information between an HTML form and ios i tryed javascrip but it isn't working?
// Init Web View
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[self addSubview:webView];
NSString *testArrayString = [webView stringByEvaluatingJavaScriptFromString:#"testString"];
NSArray *testArray = [testArrayString componentsSeparatedByString:#"#"];
NSLog(#"%#", testArray);
[webView release];
return self;
any ideas?
----EDIT----
Ok guys if u only want to make data from a form pass to a ios app you only do this to your webview
[webView setDelegate:self];
Then you can implement this methods in your program marks
like:
-(void)webViewDidFinishLoad:(UIWebView *) webView {
//implement code to do here
}
if you want to capture a for just do a webview shouldStartWithRequest like this:
-(BOOL)webView:(UIwebView *) webView shouStartWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigation{
//implement code to do afeter the user click the form don't forget to return YES or NO because it is a BOOL
}
that is how i solve it any question please do tell me i try my best to help
To pass data from UIWebView and back to your Objective-C runtime, you need to implement UIWebViewDelegates webView:shouldStartLoadingWithRequest: and pass your form-data as parameters in the URL. If you craft all your URL's with a scheme like f.ex. my-app://?parameter1=value1&parameter2=value2, you can easily filter out the URLs the web view should not load, and return NO.
EDIT:
Added example-code.
- (void)initWebView
{
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
webView.delegate = self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
if ([url.scheme isEqualToString:#"http://"]) {
return YES;
}
else if ([url.scheme isEqualToString:#"my-app://"]){
// Process data from form
return NO;
}
return YES;
}
EDIT2: You are releasing the web view in your initialisation-code. This prevents the delegate-method from being called when you are loading the form-url
EDIT3: See this question for more code-examples: Invoke method in objective c code from HTML code using UIWebView

Bugged Game Center Matchmaker appearance

I am developing multiplayer for my game and I have encountered following issue
I am using Cocos 2d 2.1 , iOS 6 and following code to show matchmaker (Landscape orientation)
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[GCHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:4 viewController:[app navController] delegate:self];
And thats how it appears
Following code is used for that function
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers
viewController:(UIViewController *)viewController
delegate:(id<GCHelperDelegate>)theDelegate {
if (!gameCenterAvailable) return;
matchStarted = NO;
self.match = nil;
self.presentingViewController = viewController;
delegate = theDelegate;
[presentingViewController dismissModalViewControllerAnimated:NO];
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
GKMatchmakerViewController *mmvc =
[[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentViewController:mmvc animated:YES completion:nil];
}
is this code in HelloWorldLayer? if so is that the first layer that the director calls? I have the same problem but it's fixes if I follow strictly the cocos2d template that was created for me. Meaning that I use the IntroLayer as the initial layer and from there it transitions into the HelloWorldLayer where I have the game center code. I think it has something to do with the director being loaded too late but I'm not sure
This happens when you try to push on a UIViewController, but you only have a UIView in your app, and no UIViewController. Let me guess, you do not have a rootViewController.
If you are working from the EAGLView template, you must declare a UIViewController for the UIView. This can be as simple as,
- (void) applicationDidFinishLaunching:(UIApplication *)application
{
window.rootViewController = [[UIViewController alloc] init];
window.rootViewController.view = glView; // MUST SET THIS UP,
// NOW THE EAGLView HAS A UIViewController.
[glView startAnimation];
}
Then, anytime you want to push on one of those GameKit UIViewControllers, use the presentViewController method of your Window object's rootViewController
For example:
[self.window.rootViewController presentViewController:vc animated:TRUE completion:^(void){puts("DONE");} ] ;
The bugginess will go away and the window will be correctly formed.

Add image to UISegmentedControl from another controller

MainViewController contains UISegmentedControl with 3 segments.
SliderSubViewController contains a UISlider and -(void) sliderValueChanged:(UISlider*)sender
I want to add a code like this in SliderSubViewController.m but I'm stuck on how to access the UISegmentedControl and add an image:
- (void)viewDidLoad
{
[super viewDidLoad];
mainController = [[MainViewController alloc] initWithNibName:nil bundle:nil];
[minPrisSlider addTarget:self
action:#selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
- (void)sliderValueChanged:(UISlider*)sender
{
NSUInteger index = (NSUInteger)(minPrisSlider.value + 0.5); // Round the number.
[minPrisSlider setValue:index animated:NO];
int progressAsInt =(int)(minPrisSlider.value + 0.5f);
NSString *newText =[[NSString alloc] initWithFormat:#"%d,-",progressAsInt];
minPrisText.text = newText;
if (minPrisSlider.value != 70) {
/*mainController.segmentedControl add main bundle image named "CheckboxChecked.png"
to current segment*/
}
}
If you have added SliderViewController as subview on MainViewController, One approach you can try is to just declare object of MainViewController in SliderViewController write #property and #synthesize, dont allocate it and while adding SliderView as subview just assign it as
sliderviewcontrollerObject.mainViewControllerObject = self;
now you can access segmentedControl from sliderview ass self.mainViewControllerObject.segmentedbar
Also you can try delegate approach.

Loading bar over status bar?

I've seen several apps do this, and i've been wondering how it's done. If you look at the Wunderkit app, when it loads something, there's a blue animated bar that slides down to cover the status bar until it finishes loading. How is this done?
you can subclass UIWindow.
And set it windowLevel = UIWindowLevelStatusBar;
code:
#interface CHLoadingWindow : UIWindow
#implementation CHLoadingWindow
- (id)init
{
self = [super init];
if (self) {
self.windowLevel = UIWindowLevelStatusBar;
// then draw your UI
}
return self;
}
#end
use it:
CHLoadingWindow *loading = [[CHLoadingWindow alloc] init];
[loading makeKeyAndVisible];
discover it:
[loading release];
loading = nil;
[[[[UIApplication sharedApplication] windows] objectAtIndex:0] makeKeyWindow];