我是 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