繁体   English   中英

UIView 加载了 Nib 自动调整大小问题

[英]UIView loaded with Nib autoresizing issue

我有一个 UIView 子类 -

@interface DatePickerPopup : UIView
    UIToolbar *toolbar;
    UIDatePicker *datePicker;
@end

@implementation

- (id)initWithFrame:(CGRect)frame
{
    NSArray *xib = 
        [[NSBundle mainBundle] 
            loadNibNamed:@"DatePickerPopup" 
                  owner:self 
                options:nil];
    self = [xib objectAtIndex:0];
    if (self) {

    }
    return self;
}
@end

笔尖看起来像 -
笔尖ib 自动调整大小

在我的 UIViewController 中包含 DatePickerPopup (datePopup):

- (void)viewDidLoad
{
    datePopup = [[DatePickerPopup alloc] initWithRect:CGRectZero];
    CGRect newFrame = datePopup.frame;
    newFrame.y = 200.0f; //lets say this aligns it to the bottom in portrait
    datePopup.frame = newFrame;

    // Normally happens when accessory button pressed but for brevity...
    [self.view.superview addSubview:datePopup];
}

- (void)willAnimateRotationToInterfaceOrientation:
    (UIInterfaceOrientation)toInterfaceOrientation 
    duration:(NSTimeInterval)duration
{
    CGRect screen = [[UIScreen mainScreen] bounds];
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || 
        toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.datePopup.frame = 
            CGRectMake(0.0f, newHeightPortrait, screen.size.width, 260.0f);
    }
    else
    {
        self.datePopup.frame = 
            CGRectMake(0.0f, newHeightLandscape, screen.size.width, 260.0f);
    }
}

然而,当方向改变视图被拉伸到屏幕边界的高度时,由于某种原因,这会被拉伸 - 导航栏......

在 viewDidLoad 之后
在 viewDidLoad 之后

willAutorotate 之后...
willAutorotate 之后...

由于您的视图 controller 似乎由导航 controller 管理,因此调用[self.view.superview addSubview:datePopup]; 将弹出窗口添加为UIViewControllerWrapperView的子视图,这是 UIKit 用于实现UINavigationController功能的私有类之一。 弄乱 UIKit 的私有视图层次结构总是有风险的。 在这种情况下,根据您看到的行为,UIKit 似乎希望UIViewControllerWrapperView的任何子视图都是视图控制器的视图,因此它会相应地调整弹出窗口的大小。

我认为解决这个问题的最安全方法是让您的视图控制器的视图成为一个包含您的 tableView 的包装器,并在必要时包含您的弹出视图。 不幸的是,使用包装视图意味着视图 controller 不能是UITableViewController 您必须将超类更改为UIViewController ,设置自定义tableView属性,并手动采用UITableViewDataSourceUITableViewDelegate协议。

注意:您可能很想将弹出框添加为 window 的子视图,但我不建议这样做,因为UIWindow只会自动旋转与视图 controller 相对应的最顶层子视图。 这意味着如果您将弹出框添加到 window,它将不会自动旋转。


编辑:顺便说一句,通过重新分配self = [xib objectAtIndex:0]; initWithFrame:中,您正在泄漏最初分配的 object。 如果你打算用这种方式重新分配self,你应该先释放现有的object。

添加

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

viewController class 中的方法并返回 YES。 如果此方法返回 YES,则设备才会支持横向。 试试这个额外的代码,看看它是否有帮助......您可以在此方法本身而不是当前方法中设置横向的帧大小。 PS:我刚刚看到您使用了 UIView 而不是 controller ...您可能想更改为 controller。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM