slimboy
slimboy

Reputation: 1653

adding constraints to navigation bar

I need to implement this design in my app, but I can't figure out a way to add this constraint to the navigation bar.

I tried adding the constraint to top layout guide with a negative offset, but the navigation bar is always on top

enter image description here

Is there a way to use NSLayoutconstraints to create this specific layout with the navigation bar?

Upvotes: 2

Views: 1372

Answers (2)

andesta.erfan
andesta.erfan

Reputation: 998

Henson Fang answer in 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)
}

Upvotes: 0

Henson Fang
Henson Fang

Reputation: 1207

To achieve this design,you can put your button on your window view by the code below.

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

In addition,you can also use

[[UIApplication sharedApplication] keyWindow]

to get the window

Upvotes: 1

Related Questions