ios – UINavigationBar titleView比栏本身高

所以我试图在UINavigationBar上做一个类似于流行的UITabBar定制的中心按钮.但它有一个小问题.

基本上一切都运行良好,我有调整大小的条形,正确显示titleView,使用AutoLayout(topLayoutGuide).甚至推动画也运作良好.但是流行动画失败了.它是截图或剪切到界限,我找不到一个好的工作,这不会让我只是想做一个自定义控件,并跳过UINavigationBar.

下面是一段视频,展示了最新动态. http://tinypic.com/r/2vl8yl1/8

下面是自定义navigationBar的一些代码

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,multiplier: 1.0,constant: 0.0).active = true
NSLayoutConstraint(item: button,attribute: .Bottom,attribute: .Height,toItem: nil,attribute: .NotAnAttribute,constant: 54.0).active = true
NSLayoutConstraint(item: button,attribute: .Width,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,action: nil)
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search,action: "next")



    let button = UIButton()
    //button.setTranslatesAutoresizingMaskIntoConstraints(false)
    button.frame = CGRectMake(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,width: 40,height: 40));
    //buttonWrapper.backgroundColor = UIColor.redColor()
    buttonWrapper.addSubview(button)

    /*NSLayoutConstraint(item: button,constant: 0.0).active = true
    NSLayoutConstraint(item: button,constant: 54.0).active = true
    NSLayoutConstraint(item: button,constant: 54.0).active = true*/

    navigationItem.titleView = buttonWrapper;
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...