ios 我需要帮助改变背景图像的一个分段控制不知何故只有红色作品

wkyowqbh  于 2023-03-05  发布在  iOS
关注(0)|答案(1)|浏览(144)

我是UIKit的新手,我正在为一个项目开发一个购物车页面,几乎所有的事情都很顺利,几乎没有什么麻烦,但是一旦我开始使用分段控件来设计它的样式,我几乎什么都做不了,因为它不接受我所做的更改。

import UIKit

class ShoppingCartViewController: UIViewController {
    
    let segmentedControl = UISegmentedControl()
    let cartView = UIView()
    let wishlistView = UIView()
    
    var customTableViewController: CustomTableViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
 
        
        // Create the table view controller
        customTableViewController = CustomTableViewController()
        customTableViewController?.view.translatesAutoresizingMaskIntoConstraints = false
        cartView.addSubview(customTableViewController!.view)
        
        // Set up segmented control
        segmentedControl.insertSegment(withTitle: "Cart", at: 0, animated: true)
        segmentedControl.insertSegment(withTitle: "Wishlist", at: 1, animated: true)
        segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), for: .valueChanged)
        segmentedControl.backgroundColor = UIColor.red
        // Set up cart view
        cartView.clipsToBounds = true
        
        // Set up table view
        customTableViewController?.view.translatesAutoresizingMaskIntoConstraints = false
        cartView.addSubview(customTableViewController!.view)
        
        // Set up wishlist view
        wishlistView.isHidden = true
        
        // Add subviews
        view.addSubview(segmentedControl)
        view.addSubview(cartView)
        view.addSubview(wishlistView)
        
        // Set up constraints
        segmentedControl.translatesAutoresizingMaskIntoConstraints = false
        cartView.translatesAutoresizingMaskIntoConstraints = false
        wishlistView.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            segmentedControl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 49),
            segmentedControl.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            segmentedControl.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
            segmentedControl.heightAnchor.constraint(equalToConstant: 40),
            
            cartView.topAnchor.constraint(equalTo: segmentedControl.bottomAnchor, constant: 16),
            cartView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            cartView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            cartView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            
            customTableViewController!.view.topAnchor.constraint(equalTo: cartView.topAnchor,constant: 28),
            customTableViewController!.view.bottomAnchor.constraint(equalTo: cartView.bottomAnchor, constant: -100),
            customTableViewController!.view.leadingAnchor.constraint(equalTo: cartView.leadingAnchor, constant: 20),
            customTableViewController!.view.trailingAnchor.constraint(equalTo: cartView.trailingAnchor),
        ])
        
        // Add back button to navigation bar
        let backButton = UIBarButtonItem(image: UIImage(systemName: "chevron.left"), style: .plain, target: self, action: #selector(backButtonTapped))
        
        navigationItem.leftBarButtonItem = backButton
    }

        
    @objc func backButtonTapped() {
        // Pop the current view controller off the navigation stack
        navigationController?.popViewController(animated: true)

        // Get a reference to the tab bar controller
        if let tabBarController = navigationController?.tabBarController {
            // Set the home view controller as the selected view controller
            tabBarController.selectedIndex = 0
        }
    }

        
    @objc func segmentedControlValueChanged(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex{
        case 0:
            cartView.isHidden = false
            wishlistView.isHidden = true
        case 1:
            cartView.isHidden = true
            wishlistView.isHidden = false
        default:
            break
        }
    }
}

特别的是当我尝试将backgroundColor更改为其他背景如白色等时,它不起作用,但当我将其更改为红色时,它却完美地工作。有人能告诉我我做错了什么吗?

9rbhqvlz

9rbhqvlz1#

当您检查如何UISegmentedControl如下图所示,您会发现有UIImageViews以上的背景,这将给予模糊效果

为了克服这种情况,你必须选择首先你要做以下事情

segmentedControl.backgroundColor = .red // of course you can replace this with white and it will work 
    segmentedControl.selectedSegmentTintColor = .green // this color for the selected segment 
    
    segmentedControl.subviews[0].alpha = 0
    segmentedControl.subviews[1].alpha = 0

但完全不建议这样做
第二个解决方案是使用此函数setBackgroundImage(_:for:barMetrics:)替换UIImageViews的图像。首先,您在资产目录中添加具有您喜欢的颜色的图像,例如,我为未选择的段添加了白色,为选定的段添加了绿色

segmentedControl.setBackgroundImage(UIImage(named: "white"), for: .normal, barMetrics: .default)
    segmentedControl.setBackgroundImage(UIImage(named: "green"), for: .selected, barMetrics: .default)
    segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.black], for: .normal)

相关问题