button 垂直分布

  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//button的类型
    button.frame = CGRectMake(100, 100,90, 90);//button的frame
   button.backgroundColor = [UIColor cyanColor];//button的背景颜色
    
//    [button setBackgroundImage:[UIImage imageNamed:@"man_64.png"] forState:UIControlStateNormal];
    
//    在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
    [button setImage:[UIImage imageNamed:@"IconHome@2x.png"] forState:UIControlStateNormal];//给button添加image
    button.imageEdgeInsets = UIEdgeInsetsMake(5,13,21,button.titleLabel.bounds.size.width);//设置image在button上的位置(上top,左left,下bottom,右right)这里可以写负值,对上写-5,那么image就象上移动5个像素
    
    [button setTitle:@"首页" forState:UIControlStateNormal];//设置button的title
    button.titleLabel.font = [UIFont systemFontOfSize:16];//title字体大小
    button.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置title在一般情况下为白色字体
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//设置title在button被选中情况下为灰色字体
     button.titleEdgeInsets = UIEdgeInsetsMake(71, -button.titleLabel.bounds.size.width-50, 0, 0);//设置title在button上的位置(上top,左left,下bottom,右right)
    
//    [button setContentEdgeInsets:UIEdgeInsetsMake(70, 0, 0, 0)];//

    
//   button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置button的内容横向居中。。设置content是title和image一起变化
    
    
    [button addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
    

转载于:https://www.cnblogs.com/nonato/p/4494736.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这个功能,你可以按照以下步骤进行操作: 1. 创建一个基于Qt的C++项目。 2. 在项目中添加一个继承自QWidget的自定义窗口类。 3. 在自定义窗口类中添加一个工具栏和一个垂直布局。 4. 在工具栏中添加一个QPushButton,用于触发显示主码流和子码流按钮的槽函数。 5. 在槽函数中,创建主码流和子码流按钮,并将它们添加到垂直布局中。 6. 设置按钮的大小根据窗格和字体大小发生变化。 以下是示例代码的一部分,用于实现上述逻辑: ```cpp #include <QtWidgets> class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr); // ... private slots: void onElectronZoomClicked(); private: QVBoxLayout *layout; QToolBar *toolBar; QPushButton *electronZoomButton; QPushButton *mainStreamButton; QPushButton *subStreamButton; }; MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { // 创建布局和工具栏 layout = new QVBoxLayout(this); toolBar = new QToolBar(this); // 创建电子放大按钮 electronZoomButton = new QPushButton("电子放大", this); // 将电子放大按钮添加到工具栏 toolBar->addWidget(electronZoomButton); // 将工具栏和布局添加到窗口中 layout->addWidget(toolBar); // 连接槽函数 connect(electronZoomButton, &QPushButton::clicked, this, &MyWidget::onElectronZoomClicked); } void MyWidget::onElectronZoomClicked() { // 清空布局中的部件 QLayoutItem* child; while ((child = layout->takeAt(0)) != nullptr) { delete child->widget(); delete child; } // 创建主码流按钮和子码流按钮 mainStreamButton = new QPushButton("主码流", this); subStreamButton = new QPushButton("子码流", this); // 设置按钮的大小根据窗格和字体大小发生变化 QFontMetrics metrics(font()); QSize buttonSize = QSize(metrics.width("子码流") + 20, metrics.height() + 10); mainStreamButton->setFixedSize(buttonSize); subStreamButton->setFixedSize(buttonSize); // 将主码流按钮和子码流按钮添加到布局中 layout->addWidget(mainStreamButton); layout->addWidget(subStreamButton); // 更新窗口布局 update(); } ``` 请注意,这只是一个示例代码片段,你需要根据自己的项目和需求进行适当的修改和补充。希望这可以帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值