简体   繁体   English

UIView xib在UIViewController中加载问题

[英]UIView xib in UIViewController loading issue

I have 3 UIViews (lets call them HeaderView , MainView and FooterView ) with UIButtons , Label , etc which I have set up in their respective xibs . 我有3个UIViews (让我们称之为HeaderViewMainViewFooterView )与UIButtonsLabel ,等我已经在各自设立xibs I have connected the elements through IBOutlets in the respective .h files. 我已经通过各自.h文件中的IBOutlets连接了元素。 In each of the .m files' drawRect , I have written some set up instructions like setting the colour of the text, setting an image for the UIButton , etc. I have a UIViewController which is presented from a button click event. 在每个.m文件的drawRect ,我编写了一些设置说明,例如设置文本的颜色,设置UIButton的图像等。我有一个UIViewController ,它是通过按钮click事件显示的。 In this UIViewController , I have called loadNib methods for the 3 UIViews with specified frame. 在此UIViewController ,我已要求loadNib为3个方法UIViews与指定帧。

The issue I'm facing is that none of the elements are being shown, for example, for one of the UIViews ( HeaderView ), the button and label is shown (checked it through ViewDebugging ) but the images or the texts aren't set for some reason. 我现在面临的问题是,没有一个元素被显示,例如,对于一个UIViewsHeaderView ),该buttonlabel所示(选中它通过ViewDebugging ),但imagestexts未设置由于某些原因。

I'm sorry if I have not explained it properly but I have tried calling addSubview and bringSubviewToFront for the UIButton and Label for the UIViews as well and it just doesn't show up. 对不起,如果我没有正确解释,但我已经打过电话addSubviewbringSubviewToFrontUIButtonLabelUIViews以及和它只是没有显示出来。 Any help would be great. 任何帮助都会很棒。 If someone could even direct me in the right direction, that would be really helpful. 如果有人甚至可以将我引导到正确的方向,那将真的很有帮助。 I have been frustratingly stuck on this and am lost as to what I should do. 我一直对此感到沮丧,并迷失了应该做的事情。

Thanks a lot for your help in advance! 非常感谢您的提前帮助!

  1. Check weather you use auto layout classes . 使用自动布局类检查天气。

  2. if yes try once by disabling them and either . 如果是,请通过禁用它们来尝试一次。

  3. Add View in ViewDidAppear Delegate Method to add view 在ViewDidAppear委托方法中添加视图以添加视图

  4. Or try on button click to add if u dont want to disable the size classes.. 或者,如果您不想禁用尺寸类别,则尝试单击按钮添加。

actually size classes have more priority then dynamic CGRectMake frames before ViewDidAppear . 实际上,尺寸类比动态CGRectMake帧在ViewDidAppear之前具有更高的优先级。 After ViewDidAppear priority goes to frames. 在ViewDidAppear之后,优先级进入帧。

.Please try this one and reply whether it works or not . 请尝试此操作,然后回复是否有效。

                    Thanks

You must take into consideration when are you creating the views from the xibs and adding them to the view hierarchy. 从xib创建视图并将它们添加到视图层次结构时,必须考虑到这一点。

Typically you must do something like: 通常,您必须执行以下操作:

@implementation MYViewController 
{
    UIView *_headerView;
    UIView *_mainView;
    UIView *_footerView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _headerView = [[[UINib nibWithNibName:@"HeaderView" bundle:nil] instantiateWithOwner:nil options:nil] firstObject];
    _mainView = [[[UINib nibWithNibName:@"MainView" bundle:nil] instantiateWithOwner:nil options:nil] firstObject];
    _footerView = [[[UINib nibWithNibName:@"FooterView" bundle:nil] instantiateWithOwner:nil options:nil] firstObject];

    _headerView.translatesAutoresizingMaskIntoConstraints = NO;
    _mainView.translatesAutoresizingMaskIntoConstraints = NO;
    _footerView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:_headerView];
    [self.view addSubview:_mainView];
    [self.view addSubview:_footerView];

    // Set here autolayouts for _headerView, _mainView and _footerView
}
@end

Also, you could configure your views as IBOutlets and use the file owner of the xib to assign the views (by setting the file owner of each XIB of header, main and footer views as a MYViewController class and assigning the view to the corresponding file's owner IBOutlet property): 另外,您可以将视图配置为IBOutlets并使用xib的文件所有者来分配视图(通过将页眉,主视图和页脚视图的每个XIB的文件所有者设置为MYViewController类,并将该视图分配给相应文件的所有者IBOutlet属性):

@implementation MYViewController 
{
    IBOutlet UIView *_headerView;
    IBOutlet UIView *_mainView;
    IBOutlet UIView *_footerView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UINib nibWithNibName:@"HeaderView" bundle:nil] instantiateWithOwner:self options:nil];
    [[UINib nibWithNibName:@"MainView" bundle:nil] instantiateWithOwner:self options:nil];
    [[UINib nibWithNibName:@"FooterView" bundle:nil] instantiateWithOwner:self options:nil];

    _headerView.translatesAutoresizingMaskIntoConstraints = NO;
    _mainView.translatesAutoresizingMaskIntoConstraints = NO;
    _footerView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:_headerView];
    [self.view addSubview:_mainView];
    [self.view addSubview:_footerView];

    // Set here autolayouts for _headerView, _mainView and _footerView
}

@end @结束

Also, check your autolayout/autoresize implementation and add some NSLog flags on your views (on the method -awakeFromNib ) to check if the view is loaded or not. 另外,检查您的自动布局/自动调整大小实现,并在视图上添加一些NSLog标志(在-awakeFromNib方法-awakeFromNib )以检查视图是否已加载。

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

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