English 中文(简体)
ios - 程序编码 UI 按钮未显示于我的视图中
原标题:ios - Programmatically coded UIButton not showing on my view

..

你好啊 你好 你好 你好 你好 你好 Hello, Hello, Hello, Hello,

我试图在我的UIView Control中加上一个UI按钮和其他项目,

- (void)viewDidLoad
{
[super viewDidLoad];



UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];    
backButton = [[UIButton alloc] init];
backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton..frame = CGRectMake(10, 25, 150, 75);
backButton..titleLabel..font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
backButton..titleLabel..text = @"Back"; 
[self..view addSubview:backButton];


// Do any additional setup after loading the view..
}

现在代码被放在了 曾经是LOAD方法的视图中, 它没有出现, 这让我想到一个问题—— 我做错什么了?

当我想改变背景的颜色 并放入视野 它的工作正常。.

干杯 杰夫

最佳回答

我认为你插入按钮可能是正确的,但只是看不到它,因为它没有背景,文字也没有显示。

尝试使用一个 圆形矩形按钮 系统按钮, 仅看它是否显示。 设置文本正确。 我也会删除字体设置, 以防字体出现问题 。

// backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
backButton = [UIButton buttonWithType:UIButtonTypeSystem];
backButton.frame = CGRectMake(10, 25, 150, 75);
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[self.view addSubview:backButton];

更新: UIButtonTypeRoundedRect 已被折旧。

问题回答

您错误地设定按钮标题。 使用 :

- (void)setTitle:(NSString *)title forState:(UIControlState)state

正如其他人所说,您不需要三次初始化按钮。 移除第二行和第三行 。

您已经使用静态方法按钮创建了按钮。WithType, 您再次为该按钮分配了内存 。

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 25, 150, 75);
backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
backButton.titleLabel.text = @"Back";
[self.view addSubview:backButton];

除了评论中所指出的问题之外

UI 按钮- 纽扣- 关闭, 创建空白按钮 。

当您想要使用图像作为按钮时使用此选项, 但请记住使用 secondimage: overs the type of the button。 因此您需要设置背景图像 。

你的按钮实际上就在那里 它只是没有"出现"到那里

使用 UI纽扣TypeRoundRoundRect (可能略有不同)

或者如果您想要一个图像作为按钮 。

Use UIButtonTypeCustom Call setBackgroundImage: then call setTitle:forState:

您必须使用按键类型是圆圆矩形, 然后我们可以看到背景颜色

[UIButton buttonWithType: UIButtonTypeRoundedRect]; 

希望它能帮上忙!

您可以在稍后拨打块块后使用调度。 所以这里先装入第一个视图, 它会在 0.1 秒内装入, 然后所有按钮在视图中可见 。 Xcode, 开始打字发送_ after 并按 Enter 键自动完成如下 :

尝试此 :

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

  UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];    
  backButton = [[UIButton alloc] init];
  backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  backButton.frame = CGRectMake(10, 25, 150, 75);
  backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  backButton.titleLabel.text = @"Back"; 
  [self.view addSubview:backButton];

 });

对于我来说,问题在于 UIButton s 文本和背景的默认颜色与我应用程序的背景颜色的结合,使得它看起来好像当时没有按钮;只是很难看到。将文字颜色设置在与应用程序背景相反的东西上,使问题固定下来。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签