开发者

Moving objects around when the view rotates

I have an iPad app that I would like to work in the sideways orientation instead of just portrait. I have programatically placed images, labels, and buttons into my view and used CGRectMake (x,x,x,x) to tell them where to go on the view into the center. When the app rotates horizontally, I need my labels and buttons to shift up (since they can't go down as far when in landscape mode), but stay in the center. Here is some code I've been playing with:

if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)) 
{
    lblDate = [[UILabel  alloc] initWithFrame:CGRectMake(384-(fieldWidth/2)-30,controlTop+45,120,40)]; //these dimensions aren't correct, though t开发者_StackOverflowhey don't matter here

    lblDate.text = @"Date:";
    lblDate.backgroundColor = [UIColor clearColor];
    [contentView addSubview:lblDate];
} else {
    //the orientation must be portrait or portrait upside down, so put duplicate the above code and change the pixel dimensions
}

Thanks for your help!


Take a look at this: iphone/ipad orientation handling

You just specify each control location depending on the rotation.


I know this might be a bit of an old question now looking to the date, but I just very recently faced the same problem. You could stumble upon many suggestions such as transforming main view's subviews or it's layers. Non of this worked for me.

Actually the solitary solution I've found is that since you want your UI controls to be located dynamically then don't deploy them mainly in the interface builder. The interface builder can be helpful knowing the desired locations for dynamic controls in both portrait and landscape orientations. i.e make two separate test views in the interface builder, one portrait and the other landscape, align your controls as you wish and right down X, Y, Width and Height data just to use with CGRectMake for each control.

As soon as you write down all needed positioning data from the interface builder get rid of those already drawn controls and outlets/actions links. They will be of no need now.

Of course don't forget to implement UIViewController's willRotateToInterfaceOrientation to set control's frame with each orientation change.

@interface

//Declare your UI control as a property of class.
@property (strong, nonatomic) UITableView *myTable;

@end

@implementation

// Synthesise it
@synthesize myTable

- (void)viewDidLoad
{
    [super viewDidLoad];

// Check to init for current orientation, don't use [UIDevice currentDevice].orientation
  if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        myTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 20, 228, 312)];
    }
    else if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        myTable = [[UITableView alloc] initWithFrame:CGRectMake(78, 801, 307, 183)];
    }
}

    myTable.delegate = self;
    myTable.dataSource = self;

    [self.view addSubview:myTable];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        // Show landscape
        myTable.frame = CGRectMake(20, 20, 228, 312);

    }
    else
    {
        // Show portrait
        myTable.frame = CGRectMake(78, 801, 307, 183);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜