通过 viewDidAppear() 或 viewWillLayoutSubviews() 设置圆角半径? [英] Setting corner radius through viewDidAppear() or viewWillLayoutSubviews()?

查看:55
本文介绍了通过 viewDidAppear() 或 viewWillLayoutSubviews() 设置圆角半径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总而言之,通过在 viewWillLayoutSubviews() 中抓取按钮边界并将高度除以 2,可以在我的应用程序中为我的 UIButton 设置圆角半径以使其左右边缘变圆.但是,如果我通过 UIBarButtonItem() 导航到另一个屏幕,旋转设备,然后使用 UIButtons() 导航回屏幕,它们不是圆形的.角半径仍然基于之前方向的按钮尺寸.我尝试使用 viewDidAppear,但在设置角半径时有第二个/明显的延迟.

有没有办法加快 viewDidAppear 的进程?我不认为 viewWillAppear 会起作用,因为视图控制器不知道我正在更改的 uibutton 的默认(方形)尺寸.

我正在开发的应用程序是这样的:

In summary, setting the corner radii to get round left and right edges for my UIButtons in my application works by grabbing the buttons bounds and dividing the height by two in viewWillLayoutSubviews(). However, if I navigate to another screen via UIBarButtonItem(), rotate the device, and then navigate back to the screen with the UIButtons() they are NOT round. The corner radii are still based on the button dimensions from the previous orientaion. I tried to use viewDidAppear, but there is a second/noticable delay in setting the corner radii.

Is there anyway to speed the process up of viewDidAppear? I don't think viewWillAppear will work because the view controller isn't aware of the default (square) dimensions of the uibutton(s) I am changing.

The application I am working on is this:

Screenshots of my current application from loading screen to the point of the corner radius not being updated correctly

解决方案

Use viewDidLayoutSubviews instead, because you'll have the updated bounds at that point, but not yet in viewWillLayoutSubviews.

Or I might suggesting have a button subclass that does the rounding within its own layoutSubviews, freeing your view controller from having to iterate through buttons and rounding them.

For example:

@IBDesignable
class RoundedButton: UIButton {

    override func layoutSubviews() {
        super.layoutSubviews()

        let radius = min(bounds.width, bounds.height) / 2
        layer.cornerRadius = radius
    }

}

And then just use this class instead of UIButton. That yields:

这篇关于通过 viewDidAppear() 或 viewWillLayoutSubviews() 设置圆角半径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆