美文网首页
iOS 小技巧的整理

iOS 小技巧的整理

作者: 阿朱先生 | 来源:发表于2018-05-24 14:46 被阅读0次

    设置按钮图片高度

    class ViewController: UIViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let btHeight: CGFloat = 100
            let demoButton = UIButton(type: .custom)
            demoButton.frame.size = CGSize(width: 50, height: btHeight)
            demoButton.setImage(UIImage(named: "img"), for: .normal)
            demoButton.setImageHeight(20, buttonHeight: btHeight)
            view.addSubview(demoButton)
            
        }
    
    }
    
    extension UIButton {
        
        func setImageHeight(_ height: CGFloat, buttonHeight: CGFloat) {
            // 如果不满足 buttonHeight >= height, 则报错
            assert(buttonHeight >= height, "图片高度大于按钮高度!请重新设置")
            assert(height > 0 && buttonHeight > 0, "高度必须大于0")
    
            let gap = (buttonHeight-height)/2
            imageView?.contentMode = .scaleAspectFit
            imageEdgeInsets = UIEdgeInsets(top: gap, left: 0, bottom: gap, right: 0)
        }
    }
    

    UINavigationController 自定义了 navigationItem.leftBarButtonItem 的滑动返回

    在 UINavigationController 中,如果 A push B, B 中自定义了 navigationItem.leftBarButtonItem

        // In B viewcontrcolleer
        override func viewDidLoad() {
            super.viewDidLoad()
            
            navigationItem.leftBarButtonItem = UIBarButtonItem(title: "XXOO", style: .plain, target: self, action: #selector(backTest))
        }
    

    那么此时在该页面的UINavigationController滑动返回是不起作用的,设置self.navigationController?.interactivePopGestureRecognizer?.delegate = self 即可正常滑动返回了

        // In B viewcontrcolleer
        override func viewDidLoad() {
            super.viewDidLoad()
            
            navigationItem.leftBarButtonItem = UIBarButtonItem(title: "XXOO", style: .plain, target: self, action: #selector(backTest))
            self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        }
    

    UINavigationBar 去除默认分割线

    extension UINavigationBar {
        
        // 去除 UINavigationBar 默认分割线
        func setWithNoSeperateLine() {
            self.setBackgroundImage(UIImage.from(color: UIColor.clear), for: .any, barMetrics: .default)
            self.shadowImage = UIImage()
        }
        
        // 使用 UINavigationBar 默认分割线
        func setWithDefaultBar() {
            self.setBackgroundImage(nil, for: .any, barMetrics: .default)
            self.shadowImage = nil
        }
    
    }
    

    相关文章

      网友评论

          本文标题:iOS 小技巧的整理

          本文链接:https://www.haomeiwen.com/subject/ajrsjftx.html