旋转回到肖像后,导航标题重叠条形按钮项目

时间:2017-02-03 23:38:40

标签: ios objective-c ios10

我试图在文本太长时截断标签的尾部。一切都有效(附截图)。旋转到横向时出现问题。导航栏扩展为覆盖左右导航栏项。旋转回肖像时出现同样的问题。我在标签和导航栏周围放置边框以帮助说明(红色表示导航栏,蓝色表示标签)。有没有办法阻止导航栏重叠左右栏项目?

        UILabel *broadcastLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        broadcastLabel.text = BILocalizedString(@"Broadcast with long titleeeeeeeeeeee");
        [broadcastLabel sizeToFit];
        broadcastLabel.textColor = BIColorForPaletteKey(BIColorPrimaryText);
        broadcastLabel.font = BIFontForStyle(BIFontFixedNavigationStyle);
        broadcastLabel.translatesAutoresizingMaskIntoConstraints = NO;

        CGSize navBarSize = self.navigationController.navigationBar.layer.frame.size;
        UIView *titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, navBarSize.width, navBarSize.height)];
        [titleView addSubview:broadcastLabel];
        self.navigationController.navigationItem.titleView = titleView;

        titleView.layer.borderWidth = 1;
        titleView.layer.borderColor = [UIColor redColor].CGColor;
        broadcastLabel.layer.borderWidth = 1;
        broadcastLabel.layer.borderColor = [UIColor blueColor].CGColor;

        [titleView addConstraint: [NSLayoutConstraint constraintWithItem:broadcastLabel
                                                               attribute:NSLayoutAttributeTop
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:titleView
                                                               attribute:NSLayoutAttributeTop
                                                              multiplier:1.f
                                                                constant:4.f]];

        [titleView addConstraint: [NSLayoutConstraint constraintWithItem:broadcastLabel
                                                               attribute:NSLayoutAttributeCenterX
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:titleView
                                                               attribute:NSLayoutAttributeCenterX
                                                              multiplier:1.f
                                                                constant:0.f]];

        [titleView addConstraint: [NSLayoutConstraint constraintWithItem:broadcastLabel
                                                               attribute:NSLayoutAttributeWidth
                                                               relatedBy:NSLayoutRelationLessThanOrEqual
                                                                  toItem:titleView
                                                               attribute:NSLayoutAttributeWidth
                                                              multiplier:1.f
                                                                constant:0.f]];
        if (navBarSize.height >= tallSize)
        {
            /* Two lines*/
            UILabel *onewayLabel = [[UILabel alloc] init];
            onewayLabel.text = BILocalizedString(@"one-way message");
            [onewayLabel sizeToFit];
            onewayLabel.textColor = BIColorForPaletteKey(BIColorPrimaryText);
            onewayLabel.font = BIFontForStyle(BIFontFixedSmallStyle);
            onewayLabel.translatesAutoresizingMaskIntoConstraints = NO;

            [titleView addSubview:onewayLabel];

            [titleView addConstraint: [NSLayoutConstraint constraintWithItem:onewayLabel
                                                                   attribute:NSLayoutAttributeCenterX
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:titleView
                                                                   attribute:NSLayoutAttributeCenterX
                                                                  multiplier:1.f
                                                                    constant:0.f]];

            [titleView addConstraint: [NSLayoutConstraint constraintWithItem:onewayLabel
                                                                   attribute:NSLayoutAttributeBottom
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:titleView
                                                                   attribute:NSLayoutAttributeBottom
                                                                  multiplier:1.f
                                                                    constant:-4.f]];

            [titleView addConstraint: [NSLayoutConstraint constraintWithItem:onewayLabel
                                                                   attribute:NSLayoutAttributeWidth
                                                                   relatedBy:NSLayoutRelationLessThanOrEqual
                                                                      toItem:titleView
                                                                   attribute:NSLayoutAttributeWidth
                                                                  multiplier:1.f
                                                                    constant:0.f]];
        }

all three screenshots

0 个答案:

没有答案