2011-08-11 69 views
2

我需要使用addSubView以相機模式添加UIImagePickerController。我爲它編寫了代碼,但它在視圖的頂部留下了空白區域。使用addSubView的UIImagePickerController在視圖頂部留下白色邊緣

-(void)viewDidAppear:(BOOL)animated{ 
NSLog(@"ViewDidAppear"); 

OverlayView *overlay = [[OverlayView alloc] 
         initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGTH)]; 

//create a new image picker instance 
picker = [[UIImagePickerController alloc]init]; 
self.picker.delegate = self; 
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ 
    //set source to video! 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    //hide all controls 
    picker.showsCameraControls = NO; 
    picker.navigationBarHidden = NO; 
    picker.toolbarHidden = YES; 
    picker.editing = NO; 
    //make the video preview full size 
    picker.wantsFullScreenLayout = YES; 
    slider.value = 1; 

    picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform,slider.value, slider.value); 
    //set our custom overlay view 
    picker.cameraOverlayView = overlay; 
    [picker.view addSubview:slider]; 
    //show picker 


    [picker.view addSubview:capture]; 
    [picker.view addSubview:library]; 
    //[picker.view addSubview:multicapture]; 

    [capture addTarget:self action:@selector(takepicture) forControlEvents:UIControlEventTouchUpInside]; 
    [library addTarget:self action:@selector(goToLibrary) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:picker.view]; 
    [picker viewWillAppear:YES]; 
    [picker viewDidAppear:YES]; 

}else{ 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Test Text" message:@"Camera is Not Availble" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 

[overlay release]; 

}

誰能請幫助我。

謝謝。

+0

你創建IB與模擬度量任何意見?我發現有時使用它們會導致複雜的視圖層次結構的奇怪對齊行爲(可能是由於我自己的錯誤結構) – mattacular

+0

我已經創建了簡單的基於標籤欄的應用程序。這個代碼寫在一個標籤上的視圖控制器上 –

+0

NSLog SCREEN_HEIGHT常量的值(你確定你拼寫正確嗎?在你的代碼中有'SCREEN_HEIGTH') – mattacular

回答

2

正如kante0所說,那個白邊是狀態欄消失了。如果你想顯示狀態欄,使用kante0的解決方案:

[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; 
[self.view addSubview:picker.view]; 

如果您不希望顯示在狀態欄(通常當你顯示相機預覽),使用這段代碼:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 
[self.view setFrame: [[UIScreen mainScreen] bounds]]; 

// Place your overlay view here 

[self.view addSubview:picker.view]; 

但你要記住,當pickerview被刪除,如果你想再次顯示狀態欄,你需要這樣做:

[picker.view removeFromSuperview]; 
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide]; 
[self.view setFrame: [[UIScreen mainScreen] applicationFrame]]; 

// If you need to place elements programmatically, do it here 
+0

我有一些使用這個問題。我已經將UIImagePickerViewController作爲子視圖添加到self.view。但是當使用UIImagePickerViewController Library時,當我點擊圖像列表視圖或滾動列表視圖時,它會隱藏/刪除所有圖像列表。那有什麼不好?你能幫我嗎? – sathiamoorthy

1

調查NSScreen獲得準確的尺寸。仔細檢查您的高度值是否正確。例如,你可以這樣做:

int width = [[NSScreen mainScreen] frame].size.width; 
int height = [[NSScreen mainScreen] frame].size.height; 
1

不是白色邊緣,是狀態欄消失。

我解決了這個問題與:

[[UIApplication的sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

相關問題