UIButton在iOS7中点击时未显示突出显示-Swift [英] UIButton not showing highlight on tap in iOS7 - Swift

查看:85
本文介绍了UIButton在iOS7中点击时未显示突出显示-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇帖子中,他们说这很友善ios 7和8中的错误的说明-轻按UITableViewCell中的按钮不会更改为突出显示.在这里,我在Objective-C中为此发布了一个答案:

From this post they said that it is kind of a bug in ios 7 and 8 - Button in UITableViewCell does not change to highlighted when tapped. Here I post one answer for this in Objective-C:

创建自定义UITableView子类和自定义UITableViewCell子类.

Create a custom UITableView subclass and custom UITableViewCell subclass.

使用此示例UITableView的initWithFrame:

Use this sample UITableView's initWithFrame:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        // iterate over all the UITableView's subviews
        for (id view in self.subviews)
        {
            // looking for a UITableViewWrapperView
            if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewWrapperView"])
            {
                // this test is necessary for safety and because a "UITableViewWrapperView" is NOT a UIScrollView in iOS7
                if([view isKindOfClass:[UIScrollView class]])
                {
                    // turn OFF delaysContentTouches in the hidden subview
                    UIScrollView *scroll = (UIScrollView *) view;
                    scroll.delaysContentTouches = NO;
                }
                break;
            }
        }
    }
    return self;
}

使用此示例UITableViewCellinitWithStyle:reuseIdentifier:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)
    {
        // iterate over all the UITableViewCell's subviews
        for (id view in self.subviews)
        {
            // looking for a UITableViewCellScrollView
            if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewCellScrollView"])
            {
                // this test is here for safety only, also there is no UITableViewCellScrollView in iOS8
                if([view isKindOfClass:[UIScrollView class]])
                {
                    // turn OFF delaysContentTouches in the hidden subview
                    UIScrollView *scroll = (UIScrollView *) view;
                    scroll.delaysContentTouches = NO;
                }
                break;
            }
        }
    }

    return self;
}

但是我不能用Swift编写.有一些问题:

However I cannot write in Swift. There are some problem:

1)我做不到

self = super.init(style: style, reuseIdentifier: reuseIdentifier) 

错误:无法在方法中分配给自身"

Error: Cannot assign to 'self' in a method

2)在Swift中,我不能做

2) In Swift, I cannot do

view.class

与目标C一样:

[view class]

我已经搜索了几个小时,但仍然找不到我想要的东西.

I have searched for hours but still cannot get what I want.

请任何人都可以在Swift中回答这个问题吗?

Please anyone can answer this in Swift?

推荐答案

感谢Schemetrical,这是适用于我的版本. (iOS 7 + 8)

Thanks to Schemetrical, this is the working version for me. (iOS 7 + 8)

首先我写了一个实用函数:

First I wrote a utility function:

class func classNameAsString(obj: AnyObject) -> String {
    return _stdlib_getDemangledTypeName(obj).componentsSeparatedByString(".").last!
}

然后我将UITableView子类化并实现它:

then I subclass UITableView and implement this:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    for view in self.subviews {
        if (Utility.classNameAsString(view) == "UITableViewWrapperView") {
            if view.isKindOfClass(UIScrollView) {
                var scroll = (view as UIScrollView)
                scroll.delaysContentTouches = false
            }
            break
        }
    }
}

我还继承了UITableViewCell并实现了这一点:

I also subclass UITableViewCell and implement this:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    for view in self.subviews {
        if (Utility.classNameAsString(view) == "UITableViewCellScrollView") {
            if view.isKindOfClass(UIScrollView) {
                var scroll = (view as UIScrollView)
                scroll.delaysContentTouches = false
            }

        }
    }
}

这篇关于UIButton在iOS7中点击时未显示突出显示-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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