【问题标题】:How to change the size of buttons constrained together depending on screen size of the device in xCode?如何根据xCode中设备的屏幕尺寸更改约束在一起的按钮的尺寸?
【发布时间】:2019-01-14 16:43:00
【问题描述】:

我制作了一个应用,其中有两个 ViewController,每个都显示不同的按钮。在第一个显示控制器上,有一个录制声音按钮、录制标签和一个停止录制按钮。录制标签“附加”到“录制声音”按钮,“停止录制”按钮“附加”到标签,所有这些都使用约束。我遇到的问题是按钮在不同设备上保持相同大小,导致界面杂乱或空白,具体取决于设备。我在互联网上尝试了不同的修复程序,但它们仅适用于一个单独的按钮,而不适用于按钮组。

我想了解一种可以根据屏幕大小改变按钮或堆栈视图大小的解决方案

我还想学习如何在我的第二个 ViewController 中使用多个堆栈视图中的按钮来执行此操作。

例子:

iPhone 4s/SE:停止录制按钮在横向模式下被切断。与屏幕大小相比,两个视图中的按钮都大了两倍。

iPad:与屏幕尺寸相比,按钮太小了。

以下是我查看并尝试过的修复:

How can I dynamically set the size of the imageView according to the size of the image

Adjust button sizes based on screen size

Imgur 拒绝了我的请求,因此我无法上传问题的图片。

【问题讨论】:

  • 检查此答案并将其应用于您的 UIButton 以代替 imageView。 stackoverflow.com/a/36702170/4910767
  • 好的,我会告诉你它是否有效。
  • 嗨,对不起,我的问题可能措辞有误。按钮框架改变但框架内按钮的图像没有变化,我想学习如何增加按钮内部的图像。

标签: ios swift xcode uibutton autolayout


【解决方案1】:

也许你可以试试这个,首先检测当前屏幕的大小,以便以编程方式修改按钮的大小以获得正确的位置。

var heightScreen: CGFloat = 0
var widthScreen: CGFloat = 0

    override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)

         let bounds = UIScreen.main.bounds
         self.heightScreen = bounds.size.height //get the height of screen
         self.widthScreen = bounds.size.width //get the width of screen

         //once you have the sizes, you can validate them, for instance..

                //for iPhone 5/5s/SE
            if self.heightScreen==568.0 && self.widthScreen==320.0 { 
                 //here you can set the correct size to your button, depending of the iPhone's screen

                // for iPhone 6/6s/7/7s
            }else if self.heightScreen==667.0 && self.widthScreen==375.0{
                //here....

                //// for iPhone 6s Plus/ 7s Plus
            }else if self.heightScreen==736.0 && self.widthScreen==414.0{
                //here....

                //for iPad Mini / Air
            }else if self.heightScreen==1024.0 && self.widthScreen==768.0{
                //here....

                //iPad Pro
            }else if self.heightScreen==1366.0 && self.widthScreen==1024.0{
                //here....
            }

    }

【讨论】:

  • 如何以编程方式更改按钮的大小
  • 嗯,用这行代码就可以了... buttonName.frame.size = CGSizeMake(100, 50) 其中参数是CGSizeMake(width, height)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 2015-10-27
  • 1970-01-01
  • 2019-03-23
  • 2015-10-29
  • 1970-01-01
相关资源
最近更新 更多