ios - UINavigationBar titleView 比栏本高度

标签 ios swift uiview uinavigationcontroller uinavigationbar

所以我尝试在 UINavigationBar 上做一个中心按钮,类似于流行的 UITabBar 自定义。但是它有一个小问题。

基本上一切正常,我调整了栏的大小,正确显示了 titleView,使用了 AutoLayout (topLayoutGuide)。甚至推送动画也运行良好。但是流行动画失败了。它是屏幕截图或剪辑到边界,我找不到一个好的解决方法,这不会让我只想制作一个自定义控件并跳过 UINavigationBar。

这是一段视频,显示了具体情况。 http://tinypic.com/r/2vl8yl1/8

下面是自定义导航栏的一些代码:

private let NavigationBarOffset: CGFloat = 10.0

class ActionNavigationBar: UINavigationBar {

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.clipsToBounds = false
        self.contentMode = .Redraw

        // Slide the original navigationBar up to make room for the actionbutton
        transform = CGAffineTransformMakeTranslation(0.0, -NavigationBarOffset)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func sizeThatFits(size: CGSize) -> CGSize {
        var expectedSize = super.sizeThatFits(size)

        // Adjust the height of the navigation bar
        expectedSize.height += NavigationBarOffset

        return expectedSize
    }
}

这是获取按钮的代码:

navigationItem.title = "Browse"
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: nil, action: nil)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "next")

let button = UIButton()
button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.setTitle("+", forState: .Normal)
button.setTitle("", forState: .Highlighted)
button.titleLabel?.font = UIFont.boldSystemFontOfSize(40.0)
button.backgroundColor = self.view.tintColor
button.layer.cornerRadius = 27.0

let buttonWrapper = UIView(frame: CGRect(x: 0, y: 0, width: 60.0, height: 60.0));
buttonWrapper.addSubview(button)

NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: buttonWrapper, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .Equal, toItem: buttonWrapper, attribute: .Bottom, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: button, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 54.0).active = true
NSLayoutConstraint(item: button, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 54.0).active = true

navigationItem.titleView = buttonWrapper;

最佳答案

我尝试在 override func viewDidLoad() 中进行以下操作。不使用自定义导航栏对我来说工作正常。

override func viewDidLoad()
{
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //self.edgesForExtendedLayout = UIRectEdge.None
    //navigationItem.title = "Browse"

    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: nil, action: nil)
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "next")



    let button = UIButton()
    //button.setTranslatesAutoresizingMaskIntoConstraints(false)
    button.frame = CGRectMake(0, 0, 40, 40)
    button.setTitle("+", forState: .Normal)
    button.setTitle("", forState: .Highlighted)
    button.titleLabel?.font = UIFont.boldSystemFontOfSize(40.0)
    button.backgroundColor = self.view.tintColor
    button.layer.cornerRadius = 20

    let buttonWrapper = UIView(frame: CGRect( 0, y: 0, width: 40, height: 40));
    //buttonWrapper.backgroundColor = UIColor.redColor()
    buttonWrapper.addSubview(button)

    /*NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: buttonWrapper, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true
    NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .Equal, toItem: buttonWrapper, attribute: .Bottom, multiplier: 1.0, constant: 0.0).active = true
    NSLayoutConstraint(item: button, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 54.0).active = true
    NSLayoutConstraint(item: button, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 54.0).active = true*/

    navigationItem.titleView = buttonWrapper;
}

iPhone 6 image

关于ios - UINavigationBar titleView 比栏本高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784798/

相关文章:

ios - 视网膜上的核心动画

ios - 如何在 Swift3 iOS 中进行 XML 解析?

ios - UIScrollView 可以滚动,但不能从用户交互中滚动

objective-c - 无法从 Objective-c 调用一些 Swift 函数(其他的可以)

swift - 试图理解 UILabel 的行为。

ios - 使用所需的动画和方向对特定 View 进行动画处理

ios - 如何在后台连续播放闹钟声音

objective-c - 如何从静态库中获取版本号?

ios - CALayer 动画的 SpringWithDamping?

ios - UIView.convert 在计算 UIWindow 中的 UITableViewCell subview 时给出错误的结果