所以我试图在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, 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;
答案 0 :(得分:2)
我试过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;
}
答案 1 :(得分:1)
一般来说, UINavigationBar hight为65.在标题为 StatusBar 之前需要大约10 yOffset。所以你可以将你的标题设置在NavigationBar的10到65 yOffset之内。按钮的最大高度为(65-10)= 55。
所以你有两个解决方案:
您可以更改按钮的高度和宽度,
let buttonWrapper = UIView(frame: CGRect(x: 0, y: 0, width: 45.0, height: 45.0));
或者您可以更改Button的yOffset值,
let buttonWrapper = UIView(frame: CGRect(x: 0, y: -8, width: 60.0, height: 60.0));