【问题标题】:iOS Component is automatically resizing but I don't know where and whyiOS 组件正在自动调整大小,但我不知道在哪里以及为什么
【发布时间】:2016-02-09 14:05:17
【问题描述】:

我正在使用this control (RDVCalendarView),我想对其进行少量定制。我想改变日历的高度,这样它就不会是整个控制器的高度,而是小一点。所以在loadView 我把代码改成这样:

CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
applicationFrame.size.height = 200;

_calendarView = [[RDVCalendarView alloc] initWithFrame:applicationFrame];
[_calendarView setAutoresizingMask:UIViewAutoresizingNone];
[_calendarView setSeparatorStyle:RDVCalendarViewDayCellSeparatorTypeHorizontal];
[_calendarView setBackgroundColor:[UIColor whiteColor]];
[_calendarView setDelegate:self];
self.view = _calendarView;

RDVCalendarView 的整个initWithFrame 方法中,我设置了正确的高度大小。但是在viewWillAppear 之后有layoutSubviews 调用并且有504 大小的高度。我不知道会发生什么,但看起来高度正在自动调整为控制器的高度。我只是不知道它可能在哪里。感谢帮助

【问题讨论】:

    标签: ios objective-c autoresizingmask


    【解决方案1】:

    通常你不直接设置 UIViewController 的主视图的框架,见https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/AdoptingaFull-ScreenLayout/AdoptingaFull-ScreenLayout.html

    相反,您只需将要调整大小的视图作为子视图 添加到视图控制器的根视图。为此,只需将方法从loadView 更改为viewDidLoad。此时,系统已经为您创建了一个默认的 UIView 并将其分配给视图控制器的view 属性。

    将您的代码更改为:

    - (void)viewDidLoad {
        CGRect calendarFrame = self.view.frame;
        calendarFrame.size.height = 200;
    
        _calendarView = [[RDVCalendarView alloc] initWithFrame:calendarFrame];
        [_calendarView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
        [_calendarView setSeparatorStyle:RDVCalendarViewDayCellSeparatorTypeHorizontal];
        [_calendarView setBackgroundColor:[UIColor whiteColor]];
        [_calendarView setDelegate:self];
    
        [self.view addSubview:_calendarView];
    }
    

    这应该可以解决问题。

    【讨论】:

    • 谢谢你帮助了我。我知道我应该使用 addSubview 方法,我只是从示例中复制代码,并没有注意到他们是这样做的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多