iOS - UIButton 不会出现在 UIView 上

Posted

技术标签:

【中文标题】iOS - UIButton 不会出现在 UIView 上【英文标题】:iOS - UIButton does not appear on UIView 【发布时间】:2013-05-18 14:51:54 【问题描述】:

这个调整应该用一个视图覆盖锁屏(确实会发生)并向视图添加一个“ios”按钮(不会发生)。这是我的代码:

#import <UIKit/UIKit.h>
#import <SpringBoard/SpringBoard.h>

@interface Themos
-(void)unlock;
@end

%hook SpringBoard
-(void)applicationDidFinishLaunching:(id)application

    %orig;

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10,10,80,30)];
    [button addTarget:self action:@selector(unlock:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"iOS" forState: UIControlStateNormal];

    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 640, 960)];
    polygonView.backgroundColor = [UIColor redColor];
    [window addSubview:polygonView];
    [polygonView addSubview:button];
    [window makeKeyAndVisible];
    [polygonView release];
    [button release];


%new(v@:)
-(void)unlock

    [self unlockWithSound:YES];

%end

提前致谢! :)

【问题讨论】:

【参考方案1】:

在窗口可见后添加子视图到窗口。

在这行代码之后添加子视图然后它就会出现。

[window makeKeyAndVisible];


-(void)applicationDidFinishLaunching:(id)application

    %orig;

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10,10,80,30)];
    [button addTarget:self action:@selector(unlock:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"iOS" forState: UIControlStateNormal];

    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    [window makeKeyAndVisible];
    UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 640, 960)];
    polygonView.backgroundColor = [UIColor redColor];
    [window addSubview:polygonView];
    [polygonView addSubview:button];
    [polygonView release];
    [button release];

【讨论】:

@Nick Aliferopoulos:现在来了吗? 非常感谢!有效! :) 现在我因为解锁设备的方式而崩溃了。你碰巧知道其他方法吗? 从中删除(解锁:)冒号并检查 it.place like (unlock)this. @Nick Aliferopoulos:发生了什么?

以上是关于iOS - UIButton 不会出现在 UIView 上的主要内容,如果未能解决你的问题,请参考以下文章

自定义 UIButton 背景图像在点击之前不会出现

iOS:如果您长按然后释放它,UIButton 文本不会立即更改

UIButton 创建后如何更改框架?

以编程方式通过 uibutton 删除 uiview

将 UIButton 放置在与 UITabBar 相同的空间中(隐藏时)

iOS 应用中自定义 UIButton 旁边出现“蓝框”