向导航栏

时间:2017-03-28 00:25:09

标签: ios swift

我需要在我的应用中实现此设计,但我无法找到将此约束添加到导航栏的方法。

我尝试将约束添加到带有负偏移的顶部布局指南,但导航栏始终位于顶部

enter image description here

有没有办法使用NSLayoutconstraints用导航栏创建这个特定的布局?

2 个答案:

答案 0 :(得分:1)

要实现此设计,您可以通过以下代码将按钮放在窗口视图中。

UIWindow *appWindow = [[[UIApplication sharedApplication] delegate] window];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];
[aButton setFrame:CGRectMake((appWindow.frame.size.width/2)-35, 20, 70, 100)];
[appWindow addSubview:aButton];

此外,您还可以使用

[[UIApplication sharedApplication] keyWindow]

获取窗口

答案 1 :(得分:0)

Henson Fang在 Swift 4.2 中的答案:

func configNavigationTitle() {
    let groupImageView = UIImageView()
    groupImageView.contentMode = .scaleAspectFit
    groupImageView.image = R.image.groupMenu()

    let titleLable = UILabel()
    titleLable.textColor = .white
    titleLable.textAlignment = .center
    titleLable.font = R.font.iranSansMobile(size: 17)
    titleLable.text = title
    titleLable.sizeToFit()

    let backView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    titleLable.frame = CGRect(x: backView.frame.size.width / 2 - titleLable.frame.size.width / 2, y: -20, width: titleLable.frame.size.width, height: titleLable.frame.size.height)
    backView.addSubview(titleLable)
    navigationItem.titleView = backView

    let window = UIApplication.shared.keyWindow
    groupImageView.frame = CGRect(x: (window?.frame.size.width)! / 2 - 20, y: 45, width: 40, height: 40)
    window?.addSubview(groupImageView)
}