美文网首页
iOS swift的基础知识点

iOS swift的基础知识点

作者: 怀心逝水 | 来源:发表于2018-08-13 16:53 被阅读116次

1.Private/FilePrivate/Public/Open 的作用和区别

FilePrivate,Open是在swift3.0之后加进去的,其实就是对swift中数据的四种修饰符的访问权限。
权限从高到低为: open > public > fileprivate > private

Private

只允许在当前类中调用,不包括 Extension 
private 现在变为了真正的私有访问控制 
用 private 修饰的方法不可以被代码域之外的地方访问

fileprivate

fileprivate 其实就是过去的 private。 
其修饰的属性或者方法只能在当前的 Swift 源文件里可以访问。 
即在同一个文件中,所有的 fileprivate 方法属性都是可以访问到的。
class PermissionObj: NSObject {
    
    private func test() {
        
        print("private method")
    }
    
    fileprivate func test1() {
        
        print("fileprivate method")
    }
    
    public func test2() {
        
        print("public method")
    }
    
    open func test3() {
        
        print("open method")
    }

}

class SubPermissionObj: PermissionObj {

    func subMethod() -> Void {
        
        test1()
        test2()
        test3()
    }
    
    override func test1() {
        
        oneMethod()
    }
    
    override func test2() {
        
    }
    
}

extension PermissionObj {
    
    fileprivate func oneMethod() {
        
    }
}

Public

可以被任何人访问。但其他module中不可以被override和继承,而在module内可以被override和继承。

Open

可以被任何人使用,包括override和继承。

2.去掉UITabBar控件上的横线

class func hideTabbarLine(tabBar: UITabBar) -> Void {
        
        let rect: CGRect = CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 49)
        UIGraphicsBeginImageContext(rect.size)
        let context: CGContext = UIGraphicsGetCurrentContext()!
        context.setFillColor(UIColor.clear.cgColor)
        context.fill(rect)
        
        let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        
        tabBar.backgroundImage = img
        tabBar.shadowImage = img
    }

当然这里面还用到了 用代码绘制纯色图片的功能

class func imageWithRect(rect: CGRect, color: UIColor) -> UIImage {
        
        UIGraphicsBeginImageContext(rect.size)
        let context: CGContext = UIGraphicsGetCurrentContext()!
        context.setFillColor(color.cgColor)
        context.fill(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image!
    }

UINavigationBar上的横线的隐藏

class func hideNaviViewLine(view: UIView) {
        
        if (view.isKind(of: UIImageView.self) && view.height <= 1.0) {
            view.isHidden = true
            return;
        }
        for subView in view.subviews {
            self.hideNaviViewLine(view: subView)
        }
    }

设置NavigationController中控制器开启全屏返回上个界面的操作

class BaseNavigation: UINavigationController, UIGestureRecognizerDelegate {
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.openFullScreenGes()
    }
    
    func openFullScreenGes() -> Void {
        
        let target = self.interactivePopGestureRecognizer?.delegate
        let handler = NSSelectorFromString("handleNavigationTransition:")
        
        let targetView = self.interactivePopGestureRecognizer?.view
        
        let fullScreenGes = UIPanGestureRecognizer.init(target: target, action: handler)
        fullScreenGes.delegate = self
        targetView?.addGestureRecognizer(fullScreenGes)
        
        self.interactivePopGestureRecognizer?.isEnabled = false
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if self.childViewControllers.count == 1 {
            return false
        }else {
            return true
        }
        
    }
    
}

设置UINavigationBar导航栏为透明

class func customizeTranslucentNavigationBar(naviBar: UINavigationBar) -> Void {
        
        naviBar.titleTextAttributes = [kCTForegroundColorAttributeName:UIColor.white] as [NSAttributedStringKey : Any]
        naviBar.tintColor = UIColor.purple
        naviBar.isTranslucent = true
        naviBar.setBackgroundImage(UIImage(), for: .default)
        naviBar.shadowImage = UIImage()
    }

相关文章

网友评论

      本文标题:iOS swift的基础知识点

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