Ich bin ein Neuling bei iOS Entwicklung und das ist meine erste Frage zu SO. Ich entschuldige mich, wenn das eine dumme Frage ist.Aktualisiere Knopfposition programmgesteuert, wenn das Gerät rotiert
Ich verwende keine XIBs. Mein Ziel ist es, einen Knopf zu nehmen und seine Position zu bewegen, wenn sich das Gerät dreht. Mein Problem ist, dass ich keinen Weg finde, den "Rahmen" der Schaltfläche auf dem Bildschirm zu aktualisieren/aktualisieren, nachdem eine Rotation stattgefunden hat.
Hier sind die 04 Dateien, die ich benutze. Ich habe mehrere andere SO-Fragen gelesen, kann dieses Problem jedoch nicht beheben. Vielen Dank für Anregungen.
LV_AppDelegate.h
#import <UIKit/UIKit.h>
//
@class LV_ViewController;
//
@interface LV_AppDelegate : UIResponder <UIApplicationDelegate>
//
@property (strong, nonatomic) UIWindow *window;
//
@property (strong, nonatomic) LV_ViewController *viewController;
//
@end
LV_AppDelegate.m
#import "LV_AppDelegate.h"
#import "LV_ViewController.h"
//
@implementation LV_AppDelegate
//
@synthesize window = _window;
@synthesize viewController = _viewController;
//
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
//
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//
NSLog(@"Entering didFinishLaunchingWithOptions");
//
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[LV_ViewController alloc] initWithNibName:@"LV_ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
//
- (void)applicationWillResignActive:(UIApplication *)application
{
}
//
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
//
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
//
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
//
- (void)applicationWillTerminate:(UIApplication *)application
{
}
//
@end
LV_ViewController.h
#import <UIKit/UIKit.h>
//
@interface LV_ViewController : UIViewController {
UIButton *button_Narr_Places;
}
//
@property (nonatomic, retain) UIButton *button_Narr_Places;
//
@end
LV_ViewController.m
#import "LV_ViewController.h"
//
//
// - - - - - Open errors: - - - - -
/*
a.) Local declaration of 'button_Narr_Places' hides instance variable.
b.) If launched in Landscape, still thinks the device is in Portrait mode, and then
rotates to Landscape.
Launched in Portrait
Moved to Landscape
c.) Can not see button once screen is rotated. How to refresh the button's "frame" position after it's updated by "willAnimateRotationToInterfaceOrientation"?
*/
// - - - - -
@implementation LV_ViewController
//
@synthesize button_Narr_Places;
//
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
//
- (void)viewDidLoad
{
[super viewDidLoad];
}
//
- (void)loadView
{
// - - Create the "view" canvas - -
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
view.backgroundColor = [UIColor whiteColor];
//
// - - - - - Create a button - - - - -
UIButton *button_Narr_Places = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//
// - - - Adjust position based on INITIAL device orientation - - -
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
//
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
NSLog(@"Launched in Portrait");
button_Narr_Places.frame = CGRectMake(10, 200, 100, 40);
}
else if (orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft) {
NSLog(@"Launched in Landscape");
button_Narr_Places.frame = CGRectMake(50, 400, 100, 40);
}
// - - - Add button's title, color, selector - - -
[button_Narr_Places setTitle:@"Places Lived" forState:UIControlStateNormal];
button_Narr_Places.backgroundColor = [UIColor clearColor];
button_Narr_Places.tag = 2000;
[button_Narr_Places addTarget:self action:@selector(method_Narr_Places:)
forControlEvents:UIControlEventTouchUpInside];
//
[view addSubview:button_Narr_Places];
//
// -- Create a "label" view:
CGRect frame = CGRectMake(10, 85, 300, 20);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"Verdana" size:20];
label.text = @"Test ViewController.";
label.tag = 1000;
//
[view addSubview:label];
[label release];
self.view = view;
}
//
// - - - - - Method called by button above - - - - -
- (IBAction)method_Narr_Places:(id)sender
{
NSLog(@"Entering method_Narr_Places");
}
//
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"Moved to Portrait");
//
// [button_Narr_Places setFrame:CGRectMake(10, 778, 100, 40)];
button_Narr_Places.frame = CGRectMake(10, 200, 100, 40);
}
else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
NSLog(@"Moved to Portrait UpsideDown");
}
//
else if (orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft) {
NSLog(@"Moved to Landscape");
//
// [button_Narr_Places setFrame:CGRectMake(778, 100, 100, 40)];
button_Narr_Places.frame = CGRectMake(50, 400, 100, 40);
}
}
//
// - - - - - Boilerplate methods below - - - - -
//
- (void)viewDidUnload
{
[super viewDidUnload];
}
//
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
//
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
//
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
//
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
//
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
//
@end
Danke. Ihr Vorschlag löste mein Problem und löschte eine Reihe von {lokalen Deklarationen von 'button_Narr_Places' verbirgt Instanzvariablen.} Warnmeldungen. – JCL