如何使用自动布局

时间:2015-11-10 08:19:29

标签: ios objective-c

嗨,我是ios的新项目,我需要使用自动布局添加UINavigationBar标题,为此我已经使用自动布局编写了所有我的UInavigation条标题属性,但是当我运行程序显示异常时,请帮助我< / p>

我的主要课程: -

#import "ViewController.h"

@interface ViewController (){

    Back * bg;
    UINavigationController * bar;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    bg = [[Back alloc]init];

    self.navigationItem.titleView = [bg NavigationBar:bar];
}

我的backGroundClass: -

   #import "Back.h"

@implementation Back

UILabel * middle;
UILabel * left;
UILabel * right;
NSDictionary * viewsDic;
UINavigationController * MainnavigationBar;

-(UILabel*)NavigationBar :(UINavigationController*)navigationBar{

    MainnavigationBar = navigationBar;

    middle = [[UILabel alloc]init];
    middle.backgroundColor = [UIColor orangeColor];
    middle.translatesAutoresizingMaskIntoConstraints = NO;
    [MainnavigationBar.navigationBar addSubview:middle];

    viewsDic = NSDictionaryOfVariableBindings(middle);

    [MainnavigationBar.navigationBar addConstraint:[NSLayoutConstraint constraintWithItem:middle
                                                                                attribute:NSLayoutAttributeCenterX
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:MainnavigationBar.navigationBar
                                                                                attribute:NSLayoutAttributeCenterX
                                                                               multiplier:1
                                                                                 constant:0]];

    [MainnavigationBar.navigationBar addConstraint:[NSLayoutConstraint constraintWithItem:middle
                                                                                attribute:NSLayoutAttributeCenterY
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:MainnavigationBar.navigationBar
                                                                                attribute:NSLayoutAttributeCenterY
                                                                               multiplier:1
                                                                                 constant:0]];

    [MainnavigationBar.navigationBar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[middle(2)]"]
                                                                                            options:0
                                                                                            metrics:nil
                                                                                              views:viewsDic]];

    [MainnavigationBar.navigationBar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[middle(30)]"]
                                                                                            options:0
                                                                                            metrics:nil
                                                                                              views:viewsDic]];


    return middle;
}

@end

1 个答案:

答案 0 :(得分:0)

嘿,您可以使用ViewDidLoad或ViewWillAppear方法中的以下行设置标题视图

[self.navigationItem setTitleView:YourView];

在这里你可以将你的View视为UILabel,UIImageView,UIButton等......

不要将标签添加为UINavigationBar的子视图

抱歉我的错误请将你的BackGroundClass作为UIView 不要将BackGroundClass作为UIViewController或任何一个Controller,它必须是一个简单的UIView ... 我看到后台类有viewDidLoad令人惊讶...