Skip to content

Commit

Permalink
layout subview iOS 14 CPU percentage increasing is resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
wasim009 committed Oct 7, 2020
1 parent 0dd7156 commit b585c34
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion WMSegmentControl.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'WMSegmentControl'
s.version = '0.1.16'
s.version = '2.0'
s.summary = 'Custom segment control for iOS'

s.description = <<-DESC
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>9FCA69F5229BB70300181099</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
14 changes: 14 additions & 0 deletions WMSegmentControl/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)
return true
}

@objc func rotated() {
if UIDevice.current.orientation.isLandscape {
print("Landscape")
NotificationCenter.default.post(name: Notification.Name(rawValue: "DeviceRotated"), object: nil)

}

if UIDevice.current.orientation.isPortrait {
print("Portrait")
NotificationCenter.default.post(name: Notification.Name(rawValue: "DeviceRotated"), object: nil)
}
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Expand Down
6 changes: 3 additions & 3 deletions WMSegmentControl/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="Ily-Pw-ka1">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="Ily-Pw-ka1">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -397,7 +397,7 @@
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="AUg-fq-FTs" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="669.60000000000002" y="152.47376311844079"/>
<point key="canvasLocation" x="977" y="128"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="IbP-eN-PTg">
Expand Down
3 changes: 2 additions & 1 deletion WMSegmentControl/SegmentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SegmentViewController: UITableViewController {
@IBOutlet weak var sgImageOnly: WMSegment!
@IBOutlet weak var sgTextOnlyBar: WMSegment!
@IBOutlet weak var sgImageOnlyBar: WMSegment!

override func viewDidLoad() {
super.viewDidLoad()
self.title = "Custom Segment"
Expand All @@ -25,7 +26,7 @@ class SegmentViewController: UITableViewController {
sgTextOnlyBar.selectorType = .bottomBar
sgTextOnlyBar.SelectedFont = UIFont(name: "ChalkboardSE-Bold", size: 15)!
sgTextOnlyBar.normalFont = UIFont(name: "ChalkboardSE-Regular", size: 15)!

//Using callbacks
sgTextOnly.onValueChanged = { index in
print("I have selected index \(index) from WMSegment!")
Expand Down
14 changes: 12 additions & 2 deletions WMSegmentControl/Source/WMSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ open class WMSegment: UIControl {

public var animate: Bool = true

private var isFrameAreSet = false
open override func awakeFromNib() {
super.awakeFromNib()

Expand All @@ -127,7 +128,8 @@ open class WMSegment: UIControl {
self.clipsToBounds = true
buttons.removeAll()
subviews.forEach({$0.removeFromSuperview()})

NotificationCenter.default.removeObserver("DeviceRotated")
NotificationCenter.default.addObserver(self, selector: #selector(setViewLayout), name: NSNotification.Name(rawValue: "DeviceRotated"), object: nil)
if self.type == .normal {
buttons = getButtonsForNormalSegment()
} else if self.type == .imageOnTop {
Expand Down Expand Up @@ -230,11 +232,19 @@ open class WMSegment: UIControl {

open override func layoutSubviews() {
super.layoutSubviews()
setupSelector()
if isFrameAreSet == false {
setViewLayout()
}
}

@objc open func setViewLayout() {
layoutIfNeeded()
updateView()
let _animated = self.animate
self.animate = false
setSelectedIndex(self.selectedSegmentIndex)
self.animate = _animated
isFrameAreSet = true
}

@objc func buttonTapped(_ sender: UIButton) {
Expand Down

0 comments on commit b585c34

Please sign in to comment.