2

我的代码是这样的:

- (void)viewDidLoad
{
  [self setGridView];
}
-(void)setGridView
{
  CGRect frame; 
  frame .origin.x=0;
  frame.origin.y=20;
  frame.size.width=GRID_WEIGHT;
  frame.size.height=GRID_HEIGHT;
  GridView *ObjGridView=[[GridView alloc]initWithFrame:frame]; 

  [[NSBundle mainBundle ] loadNibNamed:@"GridView" owner:ObjGridView options:nil];
  [ObjGridView setGridViewFrame:frame];

  [self.view addSubview:ObjGridView.GridCellView];
  frame .origin.x+=GRID_WEIGHT;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return YES;
}

此代码将子视图添加到视图并设置框架

我的问题:1-当方向(横向或纵向)发生时我如何刷新我的视图,因为我在横向模式下设置了子视图的框架,我也想在我的纵向视图中使用理智的视图。(基本上我在哪里调用这个 -(void)setGridView 委托方法)?

2-我怎么知道,我的子视图超出了视图的范围,以便我可以在我的 setGridView 方法中处理子视图?

4

5 回答 5

2

1.当你的方向改变时,下面的方法会自动调用。根据每个方向进行必要的更改。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

if (interfaceOrientation == UIInterfaceOrientationPortrait) {}

else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {} 

else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {} 

else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {}

return YES;

}

2.您应该知道视图的宽度和高度并相应地设置框架。这没什么大不了的。

希望这会有所帮助。

于 2012-05-30T05:16:11.897 回答
1

我自己正在学习 iOS 应用程序开发的来龙去脉,所以请原谅我的简短回复。

我相信您可以在 Apple 开发人员资源的本文档中标题为“响应方向更改”的部分中找到问题的答案:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html

我希望这可以帮助您推断出解决问题的方法。

于 2012-05-30T04:30:50.687 回答
1

viewDidLoad

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

通知您方向已更改的方法:-

-(void)OrientationChange:(NSNotification*)notification
{
    UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation];

    if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight)
    {
        NSLog(@"Landscape");
    }
    else if(Orientation==UIDeviceOrientationPortrait)
    {
        NSLog(@"Portrait");
    }
}
于 2016-02-17T06:49:14.087 回答
0

回答你的问题L

关于调整方向变化的大小:如果您相应地设置弹簧和支柱,它应该自动调整大小,或者您可以按照 deamonsarea 的回答在代码中执行此操作

要检查视图是否超出了父视图的边界,请使用 CGRectContainsRect 之类的东西。

CGRect frame0 = self.view.bounds;
CGRect frame1 = ObjGridView.frame;
if(CGRectContainsRect(frame0,frame1)==NO){
  NSLog(@"exceeds bounds")
}

还注意到您没有调用 [super viewDidLoad] 和这一行

[[NSBundle mainBundle ] loadNibNamed:@"GridView" owner:ObjGridView options:nil];

加载视图的新实例,但您没有在任何地方引用它

于 2012-05-30T05:33:39.783 回答
0

我在寻找一种方法来对 UIView 本身内部的方向变化做出反应时发现了这个问题。万一还有人来...

如果您想对 a 内部的方向变化做出反应UIView,而不是 a UIViewController(出于封装或其他原因),您可以使用此方法:

class MyView: UIView {
  override func layoutSubviews() {
    super.layoutSubviews()
    println("orientation or other bounds-impacting change")
  }
}
于 2015-06-18T17:56:35.053 回答