iPad 编程 分割视图

        

    

      上图就是iPad 中分割视图的应用,左右视图均是  u i tableview 


下图是  demo  的  效果截图 :




#import "AppDelegate.h"
#import "LeftViewController.h"
#import "RightViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    NSMutableArray *array = [NSMutableArray array];
    
    //  左  实质是  uitableviewcontroller
    LeftViewController *leftController = [[LeftViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:leftController];
    [array addObject:navi];
    
    // 右
    RightViewController *rightController = [[RightViewController alloc] init];
    navi = [[UINavigationController alloc] initWithRootViewController:rightController];
    [array addObject:navi];
    
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        NSLog(@"ipad");
    }else{
        NSLog(@"not ipad");
    }
    
    /**
     *  ipad 编程,特有视图 : 分割视图
     *  类似 tabbarController 有 viewControlers属性
     *  视图排列:从左向右
     */
    UISplitViewController *svc = [[UISplitViewController alloc] init];
    svc.viewControllers = array;
    svc.delegate = rightController;
    self.window.rootViewController = svc;
    
    [self.window makeKeyAndVisible];
    return YES;
}

@end



#import <UIKit/UIKit.h>

@interface LeftViewController : UITableViewController

@end



#import <UIKit/UIKit.h>

/*
 *   一定要遵守协议,否则  svc.delegate = rightController;  报警告
 */
@interface RightViewController : UIViewController <UISplitViewControllerDelegate>

@end

#import "RightViewController.h"

@interface RightViewController ()<UISplitViewControllerDelegate,UITextFieldDelegate>
{
    //  弹出框
    UIPopoverController *_popoverCtl;
    
    UITextField *textField1;
}
@end

@implementation RightViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 定制弹出框为  tabbleview;
    UITableViewController * tableViewConntroller = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    _popoverCtl = [[UIPopoverController alloc] initWithContentViewController:tableViewConntroller];
    _popoverCtl.popoverContentSize = CGSizeMake(200, 300);
    
    //  设置 点击 rightBarButtonItem 时,推出 弹出框  _popoverCtl
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showPopover:)];
    self.navigationItem.rightBarButtonItem = buttonItem;
    
    
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
    textField1.delegate = self;
    textField1.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:textField1];

}


//  点击 rightBarButtonItem,  推出弹出框
-(void)showPopover:(id)sender
{
    // 两种方法
    //_popoverController presentPopoverFromBarButtonItem:buttonItem permittedArrowDirections:<#(UIPopoverArrowDirection)#> animated:<#(BOOL)#>
    
    //popover presentPopoverFromRect:<#(CGRect)#> inView:<#(UIView *)#> permittedArrowDirections:<#(UIPopoverArrowDirection)#> animated:<#(BOOL)#>

    _popoverCtl.popoverContentSize = CGSizeMake(100, 100);
    
    [_popoverCtl presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

#pragma mark - UITextFieldDelegate

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    _popoverCtl.popoverContentSize = CGSizeMake(200, 300);
    
    [_popoverCtl presentPopoverFromRect:CGRectMake(0, 0, 0, 40) inView:textField permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    //[_popoverCtl presentPopoverFromRect:textField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [textField1 resignFirstResponder];
}



#pragma mark -  UISplitViewControllerDelegate

-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
    return NO;//  return NO 时, 不会调用下面两个代理方法, 屏蔽掉了
}

-(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
    self.navigationItem.leftBarButtonItem = barButtonItem;
    barButtonItem.title = @"show";
    NSLog(@"will hide");
}

-(void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    NSLog(@"will show");
}


<pre name="code" class="objc">@end

 



[_popoverCtlpresentPopoverFromRect:CGRectMake(0,0,0,40)inView:textField permittedArrowDirections:UIPopoverArrowDirectionUpanimated:YES];


此句的  fromrect :  是指推出的视图frame,   其坐标是相对于  inview  来说的。 

最终显示的箭头坐标x轴 为  origin.x + wiidth/2.0  ; 

                                y轴为:origin.y + height     ;


//Popover箭头坐标,相对于inView:view这个视图

//箭头坐标FromRect:rect中的(rect.origin.x + rect.size.width / 2.0, rect.origin.y + height)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值