【问题标题】:initWithFrame and loadNibNamed not working together?initWithFrame 和 loadNibNamed 不能一起工作?
【发布时间】:2015-05-16 02:55:07
【问题描述】:

我正在使用带有嵌入式 UIPageControl 的 Paged UIScrollView,如 https://github.com/romaonthego/REPagedScrollView 中所述。

当我使用时代码运行良好

UIView *page1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, self.view.frame.size.height - 40)];
page1.backgroundColor = [UIColor lightGrayColor];
[scrollView addPage:page1];

但我需要为我的项目使用 nib 文件。我这样称呼它:

UIView *page1 = [[[NSBundle mainBundle] loadNibNamed:@"mySubViewController" owner:self options:nil] objectAtIndex: 0];
page1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, self.view.frame.size.height - 40)];
page1.backgroundColor = [UIColor lightGrayColor];
[scrollView addPage:page1];

这几乎可以正常工作,我可以调用视图,唯一的问题是视图不听 initWithFrame:CGRectMake,它会加载全屏,我再也看不到要导航的页面控件。

自动布局设置为关闭。

谁能指出我错在哪里?我能做些什么来解决这个问题?

最好的问候, 大卫

【问题讨论】:

  • 附带说明:抛弃这个过时的课程并使用UIPageViewController
  • 我愿意,但我是 iOS 的新手,这是我找到的最接近的资源。
  • 嗨,Matthias,非常感谢您的旁注!!!感觉有点愚蠢,我把工作转过来,在几分钟内完成了我必须做的事情,而我却坚持了 2 天。 UIPageViewController 很棒!!!如果有人需要,本教程对我帮助很大 (appcoda.com/uipageviewcontroller-tutorial-intro)!
  • 在 REPagedScrollView 中我唯一喜欢的是,我为每个视图 (page1) 设置了一个标识符。

标签: ios xcode uiscrollview loadnibnamed initwithframe


【解决方案1】:

您可以使用方法 -(void)setFrame:(CGRect)frame 手动设置任何视图的框架

    UIView *page1 = [[[NSBundle mainBundle] loadNibNamed:@"mySubViewController" owner:self options:nil] objectAtIndex: 0];
    page1.backgroundColor = [UIColor lightGrayColor];
    page1.frame = CGRectMake(20, 20, 280, self.view.frame.size.height - 40);
    [scrollView addPage:page1];

【讨论】:

  • 我试过了,好像不行,可能是因为代码写在里面 - (void)viewDidLoad ?
猜你喜欢
  • 1970-01-01
  • 2014-01-07
  • 2019-08-17
  • 2016-11-23
  • 2019-02-18
  • 2017-10-14
  • 2012-06-01
  • 2011-11-05
  • 2018-05-20
相关资源
最近更新 更多