自定义导航栏Swift

时间:2018-11-11 04:00:38

标签: ios swift uinavigationbar

我的应用程序中有一个导航栏,正在尝试添加一个与页面右侧齐平的右键。但是,每当我尝试将其锚定在右侧时,它仍然是侧面的16px边距。

我的代码如下:

func setupNavigationBarItems(){

    //title label
    let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width - 32, height: view.frame.height))
    //Set logo Colors
    myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSAttributedStringKey.font:UIFont(name: Helpers.Fonts.Medium, size: 24.0)!])
    myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.White, range: NSRange(location:0,length:12))
    myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: Helpers.Colors.Red, range: NSRange(location:12,length:1))
    titleLabel.attributedText = myMutableString

    //freeBusy button
    let freeBusyButton = UIButton(type: .system)
    freeBusyButton.setImage(#imageLiteral(resourceName: "free").withRenderingMode(.alwaysOriginal), for: .normal)
    freeBusyButton.frame =  CGRect(x: 0, y: 0, width: view.frame.width + 32, height: view.frame.height)
    freeBusyButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
    freeBusyButton.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
    freeBusyButton.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

    navigationItem.titleView = titleLabel
    navigationController?.navigationBar.isTranslucent = false

    navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:freeBusyButton)]

}

0 个答案:

没有答案