setBackgroundImage和setImage

本文介绍了iOS开发中UIButton使用setBackgroundImage与setImage的区别。当需要设置按钮的frame与图片大小一致时,仅使用setImage会导致frame为nil,而使用setBackgroundImage则可以解决此问题。

setBackgroundImage和setImage这两个还是有区别的

例如:

当你需要设置按钮的frame为当前图片的frame时候,如果用setImage,那么frame为nil  这时,只能用setBackgroundImage 才能使得按钮的frame为image的frame

UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setImage:[UIImage imageNamed:@"---"] forState:UIControlStateNormal];
CGRect tempFrame = backBtn.frame;
tempFrame.size = backBtn.currentBackgroundImage.size;
backBtn.frame = tempFrame;

 

此时,backBtn的frame 为nil

 

UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setBackgroundImage:[UIImage imageNamed:@"---"] forState:UIControlStateNormal];


CGRect tempFrame = backBtn.frame;
tempFrame.size = backBtn.currentBackgroundImage.size;
backBtn.frame = tempFrame;

 


此时,backBtn的frame 为image的frame
 

/** * @file CalibEnterDialog.cpp * @brief Implement the logic of the Calib Dialog (AVM) * @author(xiao.zhen@kotei.com.cn) * @version 0.1 * @date 2025-07-31 * * @copyright Copyright (c) 2025 */ #include "WindowManager.h" #include "WindowProperties.h" /** * @brief Initializes a Calib window * @return true if initialization succeeded, false otherwise * @note Ensures the Creates all Calib window successfully * @see none */ bool WindowManager::initializeCalibEnterDialog() { KWidget *pwidget = nullptr; auto &window_property_table = WindowProperties::getInstance().getMutableCalibEnterDialogPropertyTable(); for (auto &iter : window_property_table) { auto &window_id = iter.first; auto &property = iter.second; switch (property.window_style) { case eWindowStyle::kGuiDialog : /* code */ m_calib_enter_dialog = std::make_shared<KDialog>(); m_calib_enter_dialog->setBackgroundImage(property.image_normal); pwidget = m_calib_enter_dialog.get(); break; case eWindowStyle::kGuiWidget : pwidget = new KWidget(m_calib_enter_dialog.get()); pwidget->setBackgroundImage(property.image_normal); pwidget->setEnabled(false); break; case eWindowStyle::kGuiToggleButton : pwidget = new KToggleButton(m_calib_enter_dialog.get()); dynamic_cast<KToggleButton*>(pwidget)->setImage(property.image_normal, property.image_selected); break; default: break; } pwidget->setWindowId(static_cast<uint32_t>(window_id)); pwidget->setDrawMode(property.draw_mode); pwidget->setGeometry(property.x, property.y, property.width, property.height); property.widget = pwidget; } m_dialogs_table.emplace_back(m_calib_enter_dialog); m_calib_enter_dialog->registerEvent(std::bind(&WindowManager::processCalibEnterEvent, this, std::placeholders::_1)); return true; } void WindowManager::processCalibEnterEvent(KGuiEvent *event) { KToggleButton* p_select_button = dynamic_cast<KToggleButton*>(event->getObject()); if (p_select_button==nullptr) { return; } eWindowId window_id = static_cast<eWindowId>(p_select_button->getWindowId()); switch (window_id) { case eWindowId::kAutoCalib : //openloading m_calib_waiting_dialog->exec(); m_window_dialogs.push_back(m_calib_waiting_dialog); m_calib_switch.emit(window_id); m_calib_enter_dialog->done(); removeDialogFromList(m_calib_enter_dialog); // m_calib_mask->show(); break; case eWindowId::kMauanlCalib : m_calib_switch.emit(window_id); m_calib_mask->hide(); break; case eWindowId::kCalibExit : m_calib_enter_dialog->done(); removeDialogFromList(m_calib_enter_dialog); m_calib_switch.emit(window_id); m_calib_mask->hide(); break; default: break; } } void WindowManager::registerCalibChange(std::function<void(const eWindowId&)> callback) { m_calib_switch.connect(callback); } void WindowManager::notifyAutoCalibResult(bool _result) { hideCalibWaitDialog(); if(_result) { //open success dialog m_calib_succ_dialog->exec(); m_window_dialogs.push_back(m_calib_succ_dialog); // m_popup_mask->show(); } else { //open failed dialog m_calib_fail_dialog->exec(); m_window_dialogs.push_back(m_calib_fail_dialog); // m_popup_mask->show(); } } void WindowManager::showCalibEnterDialog() { m_calib_enter_dialog->exec(); m_window_dialogs.push_back(m_calib_enter_dialog); m_calib_mask->show(); }结合代码写测试文档
最新发布
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值