多个高度iOS键盘扩展

时间:2017-03-05 15:44:23

标签: ios swift

我有一个iOS键盘扩展名。我希望键盘有多个高度。根据当前任务,最多四个不同的高度。 根据设备系列,iPhone,iPhonePlus和iPad,高度很多。

我首先声明一个像这样的mainHeightConstraint

var mainHeightConstraint: NSLayoutConstraint?

然后我根据设备设置高度

 func checkDeviceSize() {
    if device.isOneOf(iphoneSeries) {
        if UIScreen.main.bounds.height > UIScreen.main.bounds.width {
            print("iPhone Potrait")
            AConstant = 280
            BConstant = 400
            CConstant = 320
            DConstant = self.view.frame.height
        } else {
            print("iPhone Landscape")
            AConstant = 220
            BConstant = 340
            CConstant = 240
            DConstant = self.view.frame.height
        }

    } else if device.isOneOf(iphonePlusSeries) {
        if UIScreen.main.bounds.height > UIScreen.main.bounds.width {
            print("regular height iphonePlus potrait")
            AConstant = 280
            BConstant = 380
            CConstant = 300
            DConstant = self.view.frame.height
        } else {
            print("regular width iphonePlus landscape")
            AConstant = 220
            BConstant = 320
            CConstant = 300
            DConstant = self.view.frame.height
        }

    } else if device.isOneOf(ipadSeries) {
        if UIScreen.main.bounds.height > UIScreen.main.bounds.width {
            print("regular height ipad potrait")
            AConstant = 280
            BConstant = 460
            CConstant = 280
            DConstant = 500
        } else {
            print("regular height ipad landscape")
            AConstant = 360
            BConstant = 440
            CConstant = 360
            DConstant = 500
        }
    }
}

并设置约束

func initialHeight() {
    self.mainHeightConstraint = NSLayoutConstraint(item: self.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: self.mainConstant)
    self.mainHeightConstraint!.priority = 1000
    self.view.addConstraint(self.mainHeightConstraint!)
    UIView.animate(withDuration: 0.2) {
        self.view.layoutIfNeeded()
    }
}

当设备旋转时,我会改变高度

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: nil) { (void) in
        self.mainHeightConstraint?.isActive = false
        self.checkDeviceSize()
        let currentHeight = (groupDefaults?.string(forKey: "CurrentHeight"))! as String
        print("\(currentHeight)")
        switch currentHeight {
        case NORMAL_HEIGHT:
            self.normalHeight()
        case EXTENDED_HEIGHT:
            self.extendedHeight()
        case MEDIUM_HEIGHT:
            self.mediumHeight()
        case FULL_HEIGHT:
            self.fullHeight()
        default: break
        }
        self.initialHeight()
        self.style()
    }
    self.view.layoutIfNeeded()
}

根据需要调用其中任何一个。

func extendedHeight() {
    print("Extended Height Set")
    self.view.removeConstraint(mainHeightConstraint!)
    groupDefaults?.set(EXTENDED_HEIGHT, forKey: CURRENT_HEIGHT)
    currentHeightMode = 3
    mainConstant = BConstant
    self.carbonTabSwipeNavigation.setTabBarHeight(0)
    initialHeight()
}

func normalHeight() {
    print("Normal Height Set")
    self.view.removeConstraint(mainHeightConstraint!)
    groupDefaults?.set(NORMAL_HEIGHT, forKey: CURRENT_HEIGHT)
    currentHeightMode = 1
    mainConstant = AConstant
    self.carbonTabSwipeNavigation.setTabBarHeight(35)
    initialHeight()
}

func mediumHeight() {
    print("Medium Height Set")
    self.view.removeConstraint(mainHeightConstraint!)
    groupDefaults?.set(MEDIUM_HEIGHT, forKey: CURRENT_HEIGHT)
    currentHeightMode = 2
    mainConstant = CConstant
    self.carbonTabSwipeNavigation.setTabBarHeight(0)
    initialHeight()

}

func fullHeight() {
    print("Full Height Set")
    self.view.removeConstraint(mainHeightConstraint!)
    groupDefaults?.set(FULL_HEIGHT, forKey: CURRENT_HEIGHT)
    currentHeightMode = 4
    mainConstant = DConstant
    self.carbonTabSwipeNavigation.setTabBarHeight(0)
    initialHeight()
}

加载视图时一切正常。高度变为各个层次。我的问题是我旋转的时候。例如,当我旋转到横向时,一切都运行良好,但当我向后旋转时,键盘的高度保持在景观高度,所有不同的高度也被打破并保持在景观高度。

我确实在日志中得到了这个

console log

告诉我,我要离开220的景观高度和我要去280的肖像高度都是活动的,xcode选择打破纵向高度。如何移除景观高度。我试着打电话

self.view.removeConstraint(mainHeightConstraint!)

调用viewWillTransition但没有骰子。

0 个答案:

没有答案