2011-08-08 31 views
5

Có cách nào để thêm một nút ở góc của một UIModalPresentationPageSheet? Ý tôi là, tôi muốn đặt nút (x) giống như Apple ở góc của Trang Trang, nhưng thêm nó vào chế độ xem gốc làm cho nó xuất hiện phía sau Trang Trang (và cũng không thể chạm vào) và thêm nó vào Trang Trang tính sẽ ẩn một phần vì nó nằm ngoài vùng xem.Thêm nút đóng vào UIModalPresentationPageSheet góc

Có giải pháp nào không?

Cảm ơn.

+0

Ý anh là gì bởi "thêm nó vào trang Sheet sẽ làm cho một phần của nó ẩn, vì nó ra khỏi khu vực xem."? –

Trả lời

1

Đây là giải pháp tôi sử dụng. Nó không hoàn toàn là những gì bạn mô tả, cũng sẽ gọn gàng, nhưng sẽ rất khó vì bạn muốn nút nằm ngoài phần giới hạn của khung nhìn (như bạn nói, nó sẽ là con của view-controller- giám sát của view).

Giải pháp của tôi là đặt nút đóng ở khu vực nút bên trái của thanh điều hướng. Tôi làm điều này automagically thông qua một phần mở rộng lớp UIViewController. Để sử dụng nó, chỉ cần gọi [currentViewController presentAutoModalViewController: modalViewController animated: YES];

@implementation UIViewController (Modal) 

- (void) presentAutoModalViewController: (UIViewController *) modalViewController withDismissAction: (SEL) onDismiss animated:(BOOL)animated 
{ 
    UINavigationController* nc = nil; 
    if (NO == [ modalViewController isKindOfClass: [UINavigationController class]]) 
    { 
     nc = [[[UINavigationController alloc] initWithRootViewController: modalViewController] autorelease]; 

     [nc setToolbarHidden:YES animated: NO]; 

     nc.modalPresentationStyle = modalViewController.modalPresentationStyle; 

     modalViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop 
                               target:self 
                               action:onDismiss] autorelease]; 
    } 
    else 
    { 
     nc = (UINavigationController*) modalViewController; 

     UIViewController* rootViewController = [nc.viewControllers objectAtIndex: 0]; 
     rootViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop 
                               target:self 
                               action:onDismiss] autorelease]; 
    } 

    [nc setNavigationBarHidden: NO]; 
    nc.navigationBar.barStyle = UIBarStyleBlack; 
    nc.toolbar.barStyle = self.navigationController.navigationBar.barStyle; 

    [self presentModalViewController: nc animated: animated ]; 
} 

- (void) presentAutoModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated 
{ 
    [self presentAutoModalViewController:modalViewController withDismissAction: @selector(autoModalViewControllerDismiss:) animated: animated]; 
} 

- (void) autoModalViewControllerDismiss: (id)sender 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (BOOL) isAutoModalViewController 
{ 
    return (self.navigationController != nil && self.navigationController.parentViewController != nil && self.navigationController.parentViewController.modalViewController == self.navigationController); 
} 

@end 
0

EDIT: Trên thực tế, điều đầu tiên tôi sẽ đề nghị bạn làm là sử dụng một loại nút đóng khác. Bạn có thể, ví dụ, thêm một thanh công cụ ở trên cùng với một nút Done.

Nếu bạn vẫn muốn X nổi kiểu Apple, hãy thử đặt thuộc tính để đảm bảo rằng nó không bị ẩn hoặc bị cắt bớt. Tôi sẽ nói cách tốt hơn để làm điều này là tạo một UIButton với hình ảnh nền trước là hình ảnh nút được tạo kiểu và hình nền mờ dần khỏi màu nền/mẫu của trang tính thành nền trong suốt xung quanh nút. Nó có hiệu quả mang lại cho bạn một "nút đóng nổi" mà không cần phải đi ra ngoài giới hạn của trang tính.

+1

clipToBounds không ảnh hưởng đến chế độ xem được hiển thị bên trong trang tính vì nó không thể mở rộng ra ngoài giới hạn của trang tính. –

0

Tôi tìm thấy đề xuất của TomSwift rất hữu ích. Đây là phiên bản sử dụng các phương pháp iOS7 không được hỗ trợ và ARC.

@implementation UIViewController (Modal) 

- (void)presentAutoModalViewController: (UIViewController *) modalViewController withDismissAction:(SEL) onDismiss animated:(BOOL)animated 
{ 
    UINavigationController* nc = nil; 
    if (NO == [ modalViewController isKindOfClass: [UINavigationController class]]) 
    { 
     nc = [[UINavigationController alloc] initWithRootViewController: modalViewController]; 
     [nc setToolbarHidden:YES animated: NO]; 
     nc.modalPresentationStyle = modalViewController.modalPresentationStyle; 

     modalViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop 
                               target:self 
                               action:onDismiss]; 
    } 
    else 
    { 
     nc = (UINavigationController*) modalViewController; 

     UIViewController* rootViewController = [nc.viewControllers objectAtIndex: 0]; 
     rootViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop 
                              target:self 
                              action:onDismiss]; 
    } 

    [nc setNavigationBarHidden: NO]; 
    nc.navigationBar.barStyle = UIBarStyleBlack; 
    nc.toolbar.barStyle = self.navigationController.navigationBar.barStyle; 

    [self presentViewController:nc animated:animated completion:nil]; 
} 

- (void)presentAutoModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated 
{ 
    [self presentAutoModalViewController:modalViewController withDismissAction: @selector(autoModalViewControllerDismiss:) animated: animated]; 
} 

- (void)autoModalViewControllerDismiss: (id)sender 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (BOOL)isAutoModalViewController 
{ 
    return (self.navigationController != nil && self.navigationController.parentViewController != nil && self.navigationController.parentViewController.presentedViewController == self.navigationController); 
} 

@end 

Và tôi gọi nó như thế ...

MyController *vc = [[MyController alloc] init]; 
vc.modalPresentationStyle = UIModalPresentationFormSheet; 
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
[self presentAutoModalViewController:info animated:YES]; 
Các vấn đề liên quan