微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐