私は iOS 開発の初心者で、これが SO に関する最初の質問です。これがばかげた質問である場合は、申し訳ありません。
私はXIBを使用していません。私の目標は、ボタンを取り、デバイスが回転したときにその位置を移動することです。私の問題は、回転が行われた後に画面上のボタンの「フレーム」を更新/更新する方法が見つからないことです。
これが私が使用している04ファイルです。他の SO の質問をいくつか読みましたが、この問題を解決できません。ご提案いただきありがとうございます。
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