簡體   English   中英

定向發生時如何刷新UIView?

[英]How do i refresh a UIView when Orientation happens?

我的代碼是這樣的:

- (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方法中處理子視圖?

1.Below方法將在您的方向改變時自動調用。 根據每個方向進行必要的更改。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

if (interfaceOrientation == UIInterfaceOrientationPortrait) {}

else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {} 

else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {} 

else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {}

return YES;

}

2.您應該知道視圖的寬度和高度,並相應地設置框架。 沒什么大不了的。

希望這會有所幫助。

我正在親自學習iOS應用程序開發的來龍去脈,所以請原諒我的簡短回答。

我相信您可以在Apple開發人員資源的本文檔中標題為“響應方向更改”的部分中找到解決問題的方法:

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

希望這有助於您推斷出解決方案。

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");
    }
}

回答您的問題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];

加載視圖的新實例,但您未在任何地方引用它

我在尋找一種對UIView自身內部的方向變化做出反應的方法時發現了這個問題。 萬一有人來...

如果要對UIView而不是UIViewController (出於封裝或其他原因)內部的方向變化做出反應,可以使用以下方法:

class MyView: UIView {
  override func layoutSubviews() {
    super.layoutSubviews()
    println("orientation or other bounds-impacting change")
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM