iOS 在scrollview加入 由自己定义的controller 生成的view

看了好久 在网上看了很多代码。。也不知道他们在说什么。。主要我想要实现的是用 scrollview   来加载  由别外的controller 生成的view  一开始是不知道怎么获取view 从网上找的例子又太深奥了  哎。。。你真是一只猪!!!要加努力才行!!

为一个controller 命名






  UIViewController* viewController1=[self.storyboard instantiateViewControllerWithIdentifier:@"navi1"]; ;
    UIView* view1 = viewController1.view;
通过以上两句就可以生成一个view 了

  要用scrollview 之前一定要先去了解一下 frame bound rect 这些坐标和相对位置的知识。。。不然要吃大亏的!

见  点击打开链接

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView = [[UIScrollView alloc] initWithFrame: self.view.bounds];//生成一个scrollview
    //为这个scrollview 设置大小与基本属性
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame) * 2, CGRectGetHeight(self.view.frame));
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
  
    //将这个scrollview 加入 原来的view 中
    [self.view addSubview: self.scrollView];
   
    //生成两个view  根据自己已经设置好的controller
    UIViewController* viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"navi1"]; ;
    UIView* view = viewController.view;
    view.userInteractionEnabled = YES;
    view.frame = self.view.bounds;
    //设置它在父视图中的位置坐标
    view.frame = CGRectOffset(self.scrollView.frame, view.frame.size.width * 0, 0);
    
    
    UIViewController* viewController1=[self.storyboard instantiateViewControllerWithIdentifier:@"navi1"]; ;
    UIView* view1 = viewController1.view;
    view1.userInteractionEnabled = YES;
    view1.frame = self.view.bounds;
    view1.frame = CGRectOffset(self.scrollView.frame, view1.frame.size.width * 1, 0);



    
    //加入scrollview中
    [self.scrollView addSubview:view];
    [self.scrollView addSubview:view1];
    //[self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width + self.scrollView.frame.size.width , 0)];
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值