我的代码是这样的:
- (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 - 当方向(横向或纵向)发生时,如何刷新视图, 因为我在lanscape模式下设置子视图的框架,我想在我的肖像视图中使用理智的视图。(基本上我在哪里调用它 - (void)setGridView委托方法)?
2 - 我怎么知道,我的子视图超出了视图范围,以便我可以在setGridView方法中处理子视图?
答案 0 :(得分:2)
1.Below方法会在您的方向发生变化时自动调用。根据每个方向进行必要的更改。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationPortrait) {}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {}
return YES;
}
2.您应该知道视图的宽度和高度,并相应地设置框架。这不是什么大问题。
希望这有用。
答案 1 :(得分:2)
在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");
}
}
答案 2 :(得分:1)
我自己正在学习iOS应用程序开发的细节,所以请原谅我的回复简洁。
我相信您可以在Apple开发者资源的本文档中标题为“响应方向变化”的部分找到您的问题的答案:
我希望这可以帮助您推断出解决问题的方法。
答案 3 :(得分:0)
回答你的问题
关于调整方向变化: 如果相应地设置弹簧和支柱,它应该自动自动调整,或者你可以根据deamonsarea的答案在代码中执行此操作
要检查视图是否超出superview的范围,请使用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];
加载一个新的视图实例,但你没有在任何地方引用它
答案 4 :(得分:0)
我在寻找一种方法来回应UIView本身内部的方向变化时发现了这个问题。如果有其他人出现......
如果您想对UIView
内的方向更改做出反应,而不是UIViewController
(出于封装或其他原因),您可以使用此方法:
class MyView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
println("orientation or other bounds-impacting change")
}
}