### Basic JXSegmentedView Setup in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Demonstrates the fundamental initialization and configuration of the JXSegmentedView component in Swift. It includes setting up the view, data source with titles, and basic styling for normal and selected states. This setup is crucial for any JXSegmentedView implementation. ```swift import UIKit import JXSegmentedView class MyViewController: UIViewController { var segmentedView: JXSegmentedView! var segmentedDataSource: JXSegmentedTitleDataSource! override func viewDidLoad() { super.viewDidLoad() // Initialize segmented view segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 88, width: view.bounds.width, height: 50)) segmentedView.delegate = self view.addSubview(segmentedView) // Configure data source segmentedDataSource = JXSegmentedTitleDataSource() segmentedDataSource.titles = ["Home", "Profile", "Settings", "About"] segmentedDataSource.titleNormalColor = .gray segmentedDataSource.titleSelectedColor = .blue segmentedDataSource.titleNormalFont = UIFont.systemFont(ofSize: 15) segmentedDataSource.isTitleColorGradientEnabled = true // Reload data and associate with view segmentedDataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = segmentedDataSource } } extension MyViewController: JXSegmentedViewDelegate { func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) { print("Selected item at index: \(index)") } } ``` -------------------------------- ### Implement JXSegmentedViewDelegate Methods in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README-English.md Provides example implementations for JXSegmentedViewDelegate methods. These methods are called when the user selects an item by clicking or scrolling, and they provide callbacks for selection and scrolling events. ```Swift //This method is called when you click to select or scroll to select. Applicable to only care about selected events, regardless of whether it is click or scroll. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {} // This method will be called when the selected condition is clicked. func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {} // This method is called when the scroll is selected. func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {} // Then callback when scrolling func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {} ``` -------------------------------- ### Initialize JXSegmentedListContainerView in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README-English.md Demonstrates how to initialize JXSegmentedListContainerView and associate it with a JXSegmentedView. This setup is crucial for enabling list lazy loading and seamless scrolling between list items. ```swift self.listContainerView = JXSegmentedListContainerView(dataSource: self) self.view.addSubview(self.listContainerView) //Associate the cotentScrollView self.segmentedView.contentScrollView = self.listContainerView.scrollView ``` -------------------------------- ### Initialize JXSegmentedView Indicator in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README-English.md Initializes and configures a JXSegmentedIndicatorLineView for JXSegmentedView. This example sets the width of the indicator line. Multiple indicators can be added to the view. ```Swift let indicator = JXSegmentedIndicatorLineView() indicator.indicatorWidth = 20 self.segmentedView.indicators = [indicator] ``` -------------------------------- ### JXSegmentedView Initialization and Configuration Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README.md Example of initializing and configuring JXSegmentedView in Swift. This includes setting the delegate, adding it as a subview, and assigning a data source with title configurations like gradient colors. ```swift segmentedView = JXSegmentedView() segmentedView.delegate = self view.addSubview(self.segmentedView) //segmentedDataSource一定要通过属性强持有,不然会被释放掉 segmentedDataSource = JXSegmentedTitleDataSource() //配置数据源相关配置属性 segmentedDataSource.titles = ["猴哥", "青蛙王子", "旺财"] segmentedDataSource.isTitleColorGradientEnabled = true //关联dataSource segmentedView.dataSource = self.segmentedDataSource ``` -------------------------------- ### Initialize JXSegmentedView in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README-English.md Initializes a JXSegmentedView instance, sets its delegate, and adds it as a subview to the main view. This is the fundamental step for using the JXSegmentedView component. ```Swift self.segmentedView = JXSegmentedView() self.segmentedView.delegate = self self.view.addSubview(self.segmentedView) ``` -------------------------------- ### JXSegmentedListContainerView Initialization and Delegate Implementation Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README.md Example of initializing JXSegmentedListContainerView and implementing its data source delegate methods. This involves setting up the list container and providing the number of lists and their corresponding view controllers or views. ```swift listContainerView = JXSegmentedListContainerView(dataSource: self) view.addSubview(self.listContainerView) //关联listContainer segmentedView.listContainer = listContainerView //实现JXSegmentedListContainerViewDataSource代理方法 //返回列表的数量 func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int { return segmentedDataSource.titles.count } //返回遵从`JXSegmentedListContainerViewListDelegate`协议的实例 func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate { return ListBaseViewController() } ``` -------------------------------- ### Swift Package Manager Installation for JXSegmentedView Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README.md Instructions for adding JXSegmentedView as a dependency using Swift Package Manager. This involves modifying the Package.swift file and building the project via the command line. ```swift dependencies: [ .package(url: "https://github.com/pujiaxin33/JXSegmentedView.git", from: "1.2.1") ] ``` ```bash $ swift build ``` -------------------------------- ### Implement JXSegmentedListContainerViewDataSource in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README-English.md Provides the necessary methods to conform to the JXSegmentedListContainerViewDataSource protocol. This involves returning the number of lists and providing instances of list view controllers or views that conform to JXSegmentedListContainerViewListDelegate. ```swift //return numbers of lists func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int { return self.segmentedDataSource.titles.count } //return the instance which comform `JXSegmentedListContainerViewListDelegate` func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate { return ListBaseViewController() } ``` -------------------------------- ### Implement JXSegmentedListContainerViewListDelegate in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README-English.md Shows how to implement the JXSegmentedListContainerViewListDelegate protocol for individual list items. This includes returning the list's view and optional methods for handling list appearance and disappearance. ```swift /// If the list is VC, return VC.view /// If the list is View, return to View itself /// - Returns: list func listView() -> UIView { return view } //Optional use, when the list is displayed func listDidAppear() {} //Optional use, called when the list disappears func listDidDisappear() {} ``` -------------------------------- ### JXSegmentedView Indicator Configuration Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README.md Code snippet demonstrating how to initialize and configure an indicator for JXSegmentedView. This example uses JXSegmentedIndicatorLineView and sets its width. ```swift let indicator = JXSegmentedIndicatorLineView() indicator.indicatorWidth = 20 segmentedView.indicators = [indicator] ``` -------------------------------- ### Configure JXSegmentedView DataSource in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README-English.md Configures the data source for JXSegmentedView using JXSegmentedTitleDataSource. This involves setting titles, enabling gradient colors for titles, and reloading the data. The dataSource must be strongly held. ```Swift //segmentedDataSource must be strongly held by the property, or it will be released self.segmentedDataSource = JXSegmentedTitleDataSource() //Configuring data source related properties self.segmentedDataSource.titles = ["猴哥", "青蛙王子", "旺财"] self.segmentedDataSource.isTitleColorGradientEnabled = true //The reloadData(selectedIndex:) method must be called, and the method will internally refresh the data source array. self.segmentedDataSource.reloadData(selectedIndex: 0) //Associated dataSource self.segmentedView.dataSource = self.segmentedDataSource ``` -------------------------------- ### Handle JXSegmentedViewDelegate Events for List Container in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README-English.md Illustrates how to forward key events from JXSegmentedView's delegate methods to JXSegmentedListContainerView. This ensures that the list container correctly responds to item clicks and scrolling gestures. ```swift func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) { //Pass the didClickSelectedItemAt event to the listContainerView, which must be called! ! ! listContainerView.didClickSelectedItem(at: index) } func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) { //Pass the scrolling event to the listContainerView, which must be called! ! ! listContainerView.segmentedViewScrolling(from: leftIndex, to: rightIndex, percent: percent, selectedIndex: segmentedView.selectedIndex) } ``` -------------------------------- ### Combine Multiple Indicators Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Shows how to apply multiple indicators to a segmented view for enhanced visual effects. This example combines a background indicator with a line indicator. Configurations for background indicator include height, color, and corner radius. Line indicator configurations include width, height, color, position, and vertical offset. `JXSegmentedIndicatorBackgroundView` and `JXSegmentedIndicatorLineView` are used. ```swift func setupMultipleIndicators() { // Background indicator let backgroundIndicator = JXSegmentedIndicatorBackgroundView() backgroundIndicator.indicatorHeight = 44 backgroundIndicator.indicatorColor = UIColor.blue.withAlphaComponent(0.1) backgroundIndicator.indicatorCornerRadius = 22 // Line indicator let lineIndicator = JXSegmentedIndicatorLineView() lineIndicator.indicatorWidth = 30 lineIndicator.indicatorHeight = 3 lineIndicator.indicatorColor = .blue lineIndicator.indicatorPosition = .bottom lineIndicator.verticalOffset = 5 // Add both indicators segmentedView.indicators = [backgroundIndicator, lineIndicator] } ``` -------------------------------- ### List Container with Lazy Loading in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Integrates JXSegmentedView with JXSegmentedListContainerView for efficient management of multiple view controllers with lazy loading. It handles view controller setup, delegation for selection and scrolling events, and provides a mechanism for creating list instances. ```swift class ContainerViewController: UIViewController { var segmentedView: JXSegmentedView! var segmentedDataSource: JXSegmentedTitleDataSource! var listContainerView: JXSegmentedListContainerView! override func viewDidLoad() { super.viewDidLoad() // Setup segmented view segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 88, width: view.bounds.width, height: 44)) segmentedView.delegate = self view.addSubview(segmentedView) segmentedDataSource = JXSegmentedTitleDataSource() segmentedDataSource.titles = ["List 1", "List 2", "List 3", "List 4"] segmentedDataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = segmentedDataSource // Setup list container with lazy loading listContainerView = JXSegmentedListContainerView(dataSource: self) listContainerView.frame = CGRect(x: 0, y: 132, width: view.bounds.width, height: view.bounds.height - 132) view.addSubview(listContainerView) // Associate content scroll view (critical for sync) segmentedView.contentScrollView = listContainerView.scrollView // Set default index listContainerView.defaultSelectedIndex = 0 } } extension ContainerViewController: JXSegmentedViewDelegate { func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) { // Must pass click events to container listContainerView.didClickSelectedItem(at: index) } func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) { // Must pass scrolling events to container listContainerView.segmentedViewScrolling(from: leftIndex, to: rightIndex, percent: percent, selectedIndex: segmentedView.selectedIndex) } } extension ContainerViewController: JXSegmentedListContainerViewDataSource { func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int { return segmentedDataSource.titles.count } func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate { // Return new view controller instance for each list let listVC = ListViewController() listVC.pageIndex = index return listVC } } // List view controller implementation class ListViewController: UIViewController, JXSegmentedListContainerViewListDelegate { var pageIndex: Int = 0 func listView() -> UIView { return view } func listDidAppear() { print("Page \(pageIndex) appeared") } func listDidDisappear() { print("Page \(pageIndex) disappeared") } } ``` -------------------------------- ### JXSegmentedView Image and Title DataSource in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Provides a Swift code example for configuring JXSegmentedView using JXSegmentedTitleImageDataSource. This allows segments to display both text titles and images, offering flexibility in UI design. Options include image position, size, spacing, and image zoom effects. ```swift func setupTitleImageSegments() { let dataSource = JXSegmentedTitleImageDataSource() dataSource.titles = ["Home", "Search", "Profile"] dataSource.normalImageInfos = ["home_normal", "search_normal", "profile_normal"] dataSource.selectedImageInfos = ["home_selected", "search_selected", "profile_selected"] // Configure image position (topImage, leftImage, bottomImage, rightImage, onlyImage) dataSource.titleImageType = .topImage dataSource.imageSize = CGSize(width: 24, height: 24) dataSource.titleImageSpacing = 8 dataSource.itemSpacing = 20 // Enable image zoom dataSource.isImageZoomEnabled = true dataSource.imageSelectedZoomScale = 1.2 // Configure title colors dataSource.titleNormalColor = .gray dataSource.titleSelectedColor = .blue dataSource.titleNormalFont = UIFont.systemFont(ofSize: 14) dataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = dataSource } ``` -------------------------------- ### Swift 列表自定义生命周期方法 Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/列表的生命周期方法处理.md 定义了列表在显示和消失过程中可选调用的生命周期方法。这些方法在列表即将出现、出现、即将消失和消失时被调用。 ```Swift /// 可选实现,列表将要显示的时候调用 @objc optional func listWillAppear() /// 可选实现,列表显示的时候调用 @objc optional func listDidAppear() /// 可选实现,列表将要消失的时候调用 @objc optional func listWillDisappear() /// 可选实现,列表消失的时候调用 @objc optional func listDidDisappear() ``` -------------------------------- ### Swift UIViewController 系统生命周期方法 Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/列表的生命周期方法处理.md 展示了UIViewController类中标准的视图生命周期方法。这些方法在视图控制器出现和消失时按特定顺序触发。 ```Swift override func viewWillAppear(_ animated: Bool) override func viewDidAppear(_ animated: Bool) override func viewWillDisappear(_ animated: Bool) override func viewDidDisappear(_ animated: Bool) ``` -------------------------------- ### Configure Gesture Recognizer Delegate for Simultaneous Recognition Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/侧滑手势处理说明文档.md This Swift code snippet illustrates how to set the delegate for the interactive pop gesture recognizer and implement the gesture recognizer delegate method to allow simultaneous recognition with other gestures. This is an additional step required when customizing the navigation bar's back item. ```Swift self.navigationController?.interactivePopGestureRecognizer?.delegate = self ``` ```Swift func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } ``` -------------------------------- ### JXSegmentedView Delegate Methods Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README.md Implementation of optional delegate methods for JXSegmentedView to handle selection and scrolling events. These methods allow responding to item selections, clicks, scrolls, and scrolling progress. ```swift //点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,而不关心具体是点击还是滚动选中的情况。 func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {} // 点击选中的情况才会调用该方法 func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {} // 滚动选中的情况才会调用该方法 func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {} // 正在滚动中的回调 func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {} ``` -------------------------------- ### Programmatic Segment Selection in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Shows how to programmatically select segments in the JXSegmentedView, with options for animated transitions or instant changes. It also includes how to retrieve the currently selected segment's index. ```swift func selectSegmentProgrammatically() { // Select with animation segmentedView.selectItemAt(index: 2) // Select without animation segmentedView.selectItemAt(index: 1, animated: false) // Get current selected index let currentIndex = segmentedView.selectedIndex print("Current index: (currentIndex)") } ``` -------------------------------- ### Dynamic Configuration for Individual Cells in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Illustrates how to dynamically configure individual cells of the JXSegmentedView based on their index. This includes customizing text colors and fonts per segment, enabling unique styling for each item. ```swift class MyViewController: UIViewController, JXSegmentedTitleDynamicConfiguration { func setupDynamicConfiguration() { segmentedDataSource = JXSegmentedTitleDataSource() segmentedDataSource.titles = ["Red", "Green", "Blue", "Yellow"] segmentedDataSource.configuration = self segmentedDataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = segmentedDataSource } // Customize colors per index func titleNormalColor(at index: Int) -> UIColor? { switch index { case 0: return .red case 1: return .green case 2: return .blue case 3: return .systemYellow default: return nil } } func titleSelectedColor(at index: Int) -> UIColor? { return titleNormalColor(at: index)?.withAlphaComponent(1.0) } func titleNormalFont(at index: Int) -> UIFont? { return index == 2 ? UIFont.boldSystemFont(ofSize: 16) : UIFont.systemFont(ofSize: 15) } } ``` -------------------------------- ### Swift: Configure Gesture Recognizer Delegate for Simultaneous Recognition Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/English/gesture.md This Swift code configures the navigation controller's interactive pop gesture recognizer to recognize gestures simultaneously with other gesture recognizers. This is necessary when customizing the navigation bar's return item and ensures that the screen edge swipe gesture works correctly in conjunction with other potential gestures. ```Swift self.navigationController?.interactivePopGestureRecognizer?.delegate = self func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } ``` -------------------------------- ### Initialize JXSegmentedListContainerView with Different List Container Types Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/1.0.0版本迁移指南.md Demonstrates how to initialize JXSegmentedListContainerView, allowing the selection of either a scrollView or a collectionView as the list container. The type parameter defaults to .scrollView, simplifying initialization when a scrollView is desired. ```swift import JXSegmentedKit // Old version initialization // init(dataSource: JXSegmentedListContainerViewDataSource) // New version initialization with default scrollView type let listContainerView = JXSegmentedListContainerView(dataSource: self) // New version initialization explicitly specifying collectionView type let listContainerView = JXSegmentedListContainerView(dataSource: self, type: .collectionView) ``` -------------------------------- ### Attributed Text Support in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Demonstrates how to use attributed strings to apply advanced styling, such as different fonts, colors, and underline styles, to segment titles. This allows for richer and more dynamic text presentation within the segmented control. ```swift func setupAttributedTitle() { let dataSource = JXSegmentedTitleAttributeDataSource() // Create attributed titles let normalAttributes: [NSAttributedString.Key: Any] = [ .font: UIFont.systemFont(ofSize: 15), .foregroundColor: UIColor.gray ] let selectedAttributes: [NSAttributedString.Key: Any] = [ .font: UIFont.boldSystemFont(ofSize: 15), .foregroundColor: UIColor.blue, .underlineStyle: NSUnderlineStyle.single.rawValue ] let titles = ["Bold", "Italic", "Underline"] dataSource.normalAttributedTitles = titles.map { NSAttributedString(string: $0, attributes: normalAttributes) } dataSource.selectedAttributedTitles = titles.map { NSAttributedString(string: $0, attributes: selectedAttributes) } dataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = dataSource } ``` -------------------------------- ### JXSegmentedListContainerViewListDelegate Methods Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/README.md Implementation of delegate methods for JXSegmentedListContainerViewListDelegate, which define the behavior and lifecycle of individual list items within the container. This includes returning the list's view and handling appearing/disappearing events. ```swift /// 如果列表是VC,就返回VC.view /// 如果列表是View,就返回View自己 /// - Returns: 返回列表视图 func listView() -> UIView { return view } func listWillAppear() {} func listDidAppear() {} func listDidDisappear() {} func listDidDisappear() {} ``` -------------------------------- ### Configure Custom Item Width and Spacing Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Demonstrates how to set custom widths for segmented items and control spacing between them. Supports fixed item widths, automatic width based on content, and even distribution of spacing. Options for `itemWidth`, `itemSpacing`, `isItemSpacingAverageEnabled`, and `itemWidthIncrement` are shown. Uses `JXSegmentedTitleDataSource`. ```swift func setupCustomWidth() { segmentedDataSource = JXSegmentedTitleDataSource() segmentedDataSource.titles = ["One", "Two", "Three"] // Option 1: Fixed width for all items segmentedDataSource.itemWidth = 100 // Option 2: Automatic width based on content (default) // segmentedDataSource.itemWidth = JXSegmentedViewAutomaticDimension // Configure spacing segmentedDataSource.itemSpacing = 15 segmentedDataSource.isItemSpacingAverageEnabled = true // Distribute evenly when content is smaller than view // Add increment to calculated width segmentedDataSource.itemWidthIncrement = 20 segmentedDataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = segmentedDataSource } ``` -------------------------------- ### Configure Triangle Indicator Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Sets up a triangle-shaped indicator for the segmented view. Allows configuration of width, height, color, position (top/bottom), vertical offset, and triangle size. The `JXSegmentedIndicatorTriangleView` is used for this. ```swift func setupTriangleIndicator() { let indicator = JXSegmentedIndicatorTriangleView() indicator.indicatorWidth = 20 indicator.indicatorHeight = 10 indicator.indicatorColor = .red // Position: .top or .bottom indicator.indicatorPosition = .bottom indicator.verticalOffset = 0 // Configure triangle shape indicator.triangleViewSize = CGSize(width: 20, height: 10) segmentedView.indicators = [indicator] } ``` -------------------------------- ### JXSegmentedView Line Indicator Configuration in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Shows how to configure and attach a JXSegmentedIndicatorLineView to the JXSegmentedView in Swift. This allows for a visual indicator, typically a line, to highlight the currently selected segment. Customization options include width, height, color, corner radius, position, and line style. ```swift func setupIndicator() { let indicator = JXSegmentedIndicatorLineView() indicator.indicatorWidth = 30 indicator.indicatorHeight = 3 indicator.indicatorColor = .blue indicator.indicatorCornerRadius = 1.5 indicator.indicatorPosition = .bottom indicator.verticalOffset = 0 // Set line style (normal, lengthen, or lengthenOffset) indicator.lineStyle = .lengthen segmentedView.indicators = [indicator] } ``` -------------------------------- ### Scroll Synchronization Delegate in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Implements the `JXSegmentedViewDelegate` to monitor and respond to scroll transitions between segments. This allows for custom logic during scrolling, such as updating related content or applying visual effects based on the scroll progress. ```swift extension MyViewController: JXSegmentedViewDelegate { func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) { print("Scrolling from (leftIndex) to (rightIndex): (percent * 100)%") // Custom transition logic based on scroll percentage if percent > 0.5 { // More than halfway to right item } else { // Still closer to left item } } func segmentedView(_ segmentedView: JXSegmentedView, canClickItemAt index: Int) -> Bool { // Prevent selection of certain items if index == 3 { return false // Disable fourth item } return true } } ``` -------------------------------- ### Add Dot Badges Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Implements a data source to show simple dot indicators for unread items or notifications. Allows customization of dot color, size, offset, and corner radius, as well as text colors and fonts. The `JXSegmentedDotDataSource` is used. Dot states can be updated dynamically. ```swift func setupDotBadge() { let dataSource = JXSegmentedDotDataSource() dataSource.titles = ["Inbox", "Sent", "Drafts", "Archive"] dataSource.dots = [true, false, true, false] // Show dots // Configure dot appearance dataSource.dotColor = .red dataSource.dotSize = CGSize(width: 8, height: 8) dataSource.dotOffset = CGPoint(x: 5, y: -3) dataSource.dotCornerRadius = 4 dataSource.titleNormalColor = .gray dataSource.titleSelectedColor = .black dataSource.titleNormalFont = UIFont.systemFont(ofSize: 15) dataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = dataSource // Update dot state dynamically DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { dataSource.dots[1] = true dataSource.reloadData(selectedIndex: segmentedView.selectedIndex) segmentedView.reloadData() } } ``` -------------------------------- ### Selecting a Specific Index in JXSegmentedView (Swift) Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/注意事项.md This code snippet shows how to programmatically select a specific item index in the JXSegmentedView. It's useful for scenarios where the UI needs to jump to a particular index based on user interaction or other application logic. ```swift segmentedView.selectItemAt(index: 3) ``` -------------------------------- ### Gradient Color Transition for Titles in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Configures a JXSegmentedTitleGradientDataSource to enable smooth color transitions between normal and selected states for segment titles. It allows customization of normal and selected colors, gradient enablement, fonts, and specific gradient colors for each item. ```swift func setupGradientTitle() { let dataSource = JXSegmentedTitleGradientDataSource() dataSource.titles = ["Featured", "Popular", "Recent", "Trending"] // Configure gradient colors for each title dataSource.titleNormalColor = .lightGray dataSource.titleSelectedColor = .black dataSource.isTitleColorGradientEnabled = true dataSource.titleNormalFont = UIFont.systemFont(ofSize: 16) // Configure custom gradients per item (optional) dataSource.titleNormalGradientColors = [ [UIColor.gray.cgColor, UIColor.lightGray.cgColor], [UIColor.gray.cgColor, UIColor.lightGray.cgColor], [UIColor.gray.cgColor, UIColor.lightGray.cgColor], [UIColor.gray.cgColor, UIColor.lightGray.cgColor] ] dataSource.titleSelectedGradientColors = [ [UIColor.blue.cgColor, UIColor.purple.cgColor], [UIColor.green.cgColor, UIColor.teal.cgColor], [UIColor.orange.cgColor, UIColor.red.cgColor], [UIColor.pink.cgColor, UIColor.red.cgColor] ] dataSource.titleGradientStartPoint = CGPoint(x: 0, y: 0) dataSource.titleGradientEndPoint = CGPoint(x: 1, y: 0) dataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = dataSource } ``` -------------------------------- ### Simplified Navigation Controller Handling for List ViewControllers Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/1.0.0版本迁移指南.md Explains that when list items are `UIViewController` instances, the custom `naviController` property is no longer required. `JXSegmentedListContainerView` now internally creates a `JXSegmentedListContainerViewController` which correctly handles adding child ViewControllers, allowing them to access `self.navigationController` directly. ```swift import JXSegmentedKit // If your list items are ViewControllers: // No need to pass a custom naviController to the list item. // The list item can directly use self.navigationController. // Example of initializing the container view: let listContainerView = JXSegmentedListContainerView(dataSource: self) // If your list items are Views, you still need to pass the navigation controller logic as before. ``` -------------------------------- ### Adjust JXSegmentedView Height with floor() in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/English/tips.md This Swift code snippet demonstrates how to adjust the height of the JXSegmentedView's internal collectionView to ensure it's an integer, preventing potential layout errors on different screen sizes. It uses `floor()` to round down the calculated height. ```Swift open override func layoutSubviews() { super.layoutSubviews() collectionView.frame = CGRect(x: 0, y: 0, width: bounds.size.width, height: floor(bounds.size.height)) } ``` -------------------------------- ### Refreshing JXSegmentedView State (Swift) Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/注意事项.md After initialization or when data sources or property configurations change (e.g., fetching new data), the `reloadData` method should be called to refresh the JXSegmentedView's state and UI. ```swift segmentedView.reloadData() ``` -------------------------------- ### Display Number Badges Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Configures a data source to display number badges alongside titles, suitable for notifications or counts. Customizes badge appearance including background color, text color, font, offset, height, width increment, and corner radius. The `JXSegmentedNumberDataSource` is used. Badges can be hidden when the count is zero. ```swift func setupNumberBadge() { let dataSource = JXSegmentedNumberDataSource() dataSource.titles = ["Messages", "Calls", "Notifications"] dataSource.numbers = [5, 12, 0] // Badge counts dataSource.titleNormalColor = .gray dataSource.titleSelectedColor = .blue // Configure number badge appearance dataSource.numberBackgroundColor = .red dataSource.numberTitleColor = .white dataSource.numberFont = UIFont.systemFont(ofSize: 11) dataSource.numberOffset = CGPoint(x: 5, y: -5) dataSource.numberHeight = 18 dataSource.numberWidthIncrement = 8 dataSource.numberCornerRadius = 9 // Hide badge when count is zero dataSource.isNumberZoomEnabled = false dataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = dataSource } ``` -------------------------------- ### Simplified ReloadData Call Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/1.0.0版本迁移指南.md Highlights the simplification of the `reloadData` method call. In previous versions, `reloadData` had to be called on both `segmentedView` and `listContainerView`. Now, calling `reloadData` on `segmentedView` is sufficient, as it internally triggers the reload for the list container. ```swift import JXSegmentedKit // Old code: // segmentedView.reloadData() // listContainerView.reloadData() // New code: segmentedView.reloadData() ``` -------------------------------- ### Enabling ContentScrollView Click Transition Animation (Swift) Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/注意事项.md The `isContentScrollViewClickTransitionAnimationEnabled` property controls the animation behavior of the contentScrollView when an item is clicked. Setting it to `true` enables scrolling animation, while `false` disables it. ```swift segmentedView.isContentScrollViewClickTransitionAnimationEnabled = true ``` -------------------------------- ### Swift: Enable Pop Gesture in ViewController Lifecycle Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/English/gesture.md This Swift code snippet enables the screen edge pop gesture recognizer only when the segmented view is on the first item (`selectedIndex == 0`). It is placed within the `viewWillAppear` method to ensure the gesture state is correctly updated when the view appears. In `viewWillDisappear`, the gesture recognizer is re-enabled to avoid affecting other pages. ```Swift override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) //Allow screen edge gestures to return when in the first item self.navigationController?.interactivePopGestureRecognizer?.isEnabled = (self.segmentedView.selectedIndex == 0) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) //When you leave the page, you need to restore the screen edge gesture, can not affect other pages self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true } ``` -------------------------------- ### Background Indicator for JXSegmentedView in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Implements a JXSegmentedIndicatorBackgroundView to add a moving background indicator behind selected segments. This includes configuration for indicator height, color, corner radius, position, and width, along with adjustments to segment item spacing and width. ```swift func setupBackgroundIndicator() { let indicator = JXSegmentedIndicatorBackgroundView() indicator.indicatorHeight = 40 indicator.indicatorColor = UIColor.blue.withAlphaComponent(0.2) indicator.indicatorCornerRadius = 20 indicator.indicatorPosition = .center // Configure width behavior indicator.indicatorWidthIncrement = 10 indicator.indicatorWidth = JXSegmentedViewAutomaticDimension segmentedView.indicators = [indicator] // Adjust data source for better appearance segmentedDataSource.itemSpacing = 10 segmentedDataSource.itemWidthIncrement = 20 } ``` -------------------------------- ### Automatic reloadData Call for JXSegmentedBaseDataSource Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/1.0.0版本迁移指南.md Details that the `reloadData(selectedIndex: Int)` method on `JXSegmentedBaseDataSource` is now automatically called internally by `segmentedView.reloadData()`. This eliminates the need for manual calls, preventing potential bugs due to forgotten invocations. ```swift import JXSegmentedKit // No longer need to manually call: // segmentedView.reloadData(selectedIndex: 0) // Just call: segmentedView.reloadData() ``` -------------------------------- ### Swift: Update Pop Gesture on Segmented View Selection Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/English/gesture.md This Swift code updates the screen edge pop gesture recognizer's enablement status whenever the selected item in the `JXSegmentedView` changes. It ensures the gesture is enabled only when the `selectedIndex` is 0, maintaining the desired navigation behavior. ```Swift func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) { self.navigationController?.interactivePopGestureRecognizer?.isEnabled = (self.segmentedView.selectedIndex == 0) } ``` -------------------------------- ### JXSegmentedView Title Zoom Effect in Swift Source: https://context7.com/pujiaxin33/jxsegmentedview/llms.txt Explains how to enable and customize the title zoom animation for selected segments in JXSegmentedView using Swift. This feature enhances user experience by visually emphasizing the active segment. Configuration includes enabling zoom, setting zoom scale, and animation duration. ```swift func setupTitleZoom() { segmentedDataSource = JXSegmentedTitleDataSource() segmentedDataSource.titles = ["News", "Sports", "Tech", "Finance"] segmentedDataSource.titleNormalFont = UIFont.systemFont(ofSize: 15) segmentedDataSource.titleSelectedFont = UIFont.systemFont(ofSize: 15) // Enable zoom effect segmentedDataSource.isTitleZoomEnabled = true segmentedDataSource.titleSelectedZoomScale = 1.3 segmentedDataSource.isSelectedAnimable = true segmentedDataSource.selectedAnimationDuration = 0.3 segmentedDataSource.reloadData(selectedIndex: 0) segmentedView.dataSource = segmentedDataSource } ``` -------------------------------- ### Introducing reloadDataWithoutListContainer Method Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/1.0.0版本迁移指南.md Introduces the new `reloadDataWithoutListContainer` method. This method allows for refreshing only the `JXSegmentedView`'s UI (e.g., updating numbers or red dots on cells) without triggering a reload of the `listContainerView`. This is useful when the list content does not need to be reloaded. ```swift import JXSegmentedKit // Call reloadData on segmentedView only, without affecting the list container segmentedView.reloadDataWithoutListContainer() ``` -------------------------------- ### Refreshing a Single Cell in JXSegmentedView (Swift) Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/注意事项.md The `reloadItem(at:)` method allows for the refreshing of a specific cell's UI at a given index. This is useful for updating individual cell elements, such as red dots or badge counts, without reloading the entire view. ```swift func reloadItem(at index: Int) ``` -------------------------------- ### Replacing contentScrollView with listContainer in JXSegmentedView Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/1.0.0版本迁移指南.md Illustrates the change in how JXSegmentedView interacts with the list container. The previous approach of directly assigning the scrollView to `contentScrollView` has been replaced by assigning the `JXSegmentedListContainerView` instance to the `listContainer` property. ```swift import JXSegmentedKit // Old code: // segmentedView.contentScrollView = listContainerView.scrollView // New code: var listContainerView = JXSegmentedListContainerView(dataSource: self) segmentedView.listContainer = listContainerView ``` -------------------------------- ### Refreshing JXSegmentedView UI Without List Container (Swift) Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/注意事项.md The `reloadDataWithoutListContainer` method is used when only the JXSegmentedView's UI needs to be refreshed, such as updating badge counts or red dots on cells, without affecting the list container. It avoids calling `listContainer.reloadData()` internally. ```swift segmentedView.reloadDataWithoutListContainer() ``` -------------------------------- ### Enable/Disable Pop Gesture in UIViewController Lifecycle Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/侧滑手势处理说明文档.md This Swift code snippet shows how to enable or disable the interactive pop gesture recognizer based on the selected index of the JXSegmentedView. It's crucial to re-enable the gesture when the view disappears to avoid affecting other pages. ```Swift override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) //处于第一个item的时候,才允许屏幕边缘手势返回 self.navigationController?.interactivePopGestureRecognizer?.isEnabled = (self.segmentedView.selectedIndex == 0) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) //离开页面的时候,需要恢复屏幕边缘手势,不能影响其他页面 self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true } ``` -------------------------------- ### Updating defaultSelectedIndex Property Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/1.0.0版本迁移指南.md Explains the change in setting the default selected index. Previously, `defaultSelectedIndex` needed to be set on both `segmentedView` and `listContainerView`. Now, it only needs to be set on `segmentedView`, as the value is automatically synchronized internally. ```swift import JXSegmentedKit // Old code: // segmentedView.defaultSelectedIndex = 1; // listContainerView.defaultSelectedIndex = 1; // New code: segmentedView.defaultSelectedIndex = 1; ``` -------------------------------- ### Disable Parent VC automaticallyAdjustsScrollViewInsets in Swift Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/English/tips.md This Swift code overrides the `willMove(toSuperview:)` method to find the parent UIViewController and set its `automaticallyAdjustsScrollViewInsets` property to `false`. This is crucial for preventing incorrect scroll view insets adjustments when JXSegmentedView is used internally. ```Swift open override func willMove(toSuperview newSuperview: UIView?) { super.willMove(toSuperview: newSuperview) var nextResponder: UIResponder? = newSuperview while nextResponder != nil { if let parentVC = nextResponder as? UIViewController { parentVC.automaticallyAdjustsScrollViewInsets = false break } nextResponder = nextResponder?.next } } ``` -------------------------------- ### JXSegmentedListContainerType.collectionView Refresh Indicator Handling (Swift) Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/注意事项.md When using JXSegmentedListContainerType.collectionView, the MJRefreshHeader's UIActivityIndicatorView can be hidden due to cell reuse. This code snippet demonstrates how to re-enable the refresh animation when the list is displayed and the refresh state is active. ```swift if refreshControl?.isRefreshing == true { refreshControl?.beginRefreshing() } ``` -------------------------------- ### Disabling Horizontal Scrolling for List Container (Swift) Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/注意事项.md This code disables the horizontal scrolling capability of the list container, which is necessary for implementing cell-level swipe-to-delete gestures. This ensures that swipe gestures are handled by the list cells rather than the container. ```swift isListHorizontalScrollEnabled = false ``` -------------------------------- ### Disable List Container Horizontal Scrolling (Swift) Source: https://github.com/pujiaxin33/jxsegmentedview/blob/master/Document/注意事项.md To enable cell swipe-to-delete functionality when the list container can scroll horizontally, this code disables the list container's horizontal scrolling. This ensures that swipe gestures are directed to the list cells. ```swift listContainerView.scrollView.scrollEnabled = NO; ```