小编Jus*_*zic的帖子

将UIButton上的文本和图像与imageEdgeInsets和titleEdgeInsets对齐

我想在两行文本左侧放置一个图标,使得图像和文本开头之间有大约2-3个像素的空间.控件本身是水平居中对齐的(通过Interface Builder设置)

该按钮类似于以下内容:

|                  |
|[Image] Add To    |
|        Favorites |
Run Code Online (Sandbox Code Playgroud)

我试图用contentEdgeInset,imageEdgeInsets和titleEdgeInsets配置它无济于事.我知道负值会扩大边缘,而正值会缩小它以使其更接近中心.

我试过了:

[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width, 0, 0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, button.titleLabel.bounds.size.width, 0, 0)];
Run Code Online (Sandbox Code Playgroud)

但这不能正确显示.我一直在调整这些值,但是从左边的插入值开始说-5到-10似乎没有按照预期的方式移动它.-10会将文本一直向左移动,所以我预计-5会将它从左侧拉出一半,但事实并非如此.

插图背后的逻辑是什么?我不熟悉图像放置和相关术语.

我用这个SO问题作为参考,但关于我的价值观的事情是不对的. UIButton:如何使用imageEdgeInsets和titleEdgeInsets使图像和文本居中?

iphone uibutton uikit ios

239
推荐指数
11
解决办法
20万
查看次数

点击X按钮时从UISearchBar中解除键盘

我正在使用UISearchBar(但不是通常联合使用的SearchDisplayController),当你点击'X'按钮时我想解除键盘.

我遵循了TomSwift的建议,当'X'被敲击并且效果很好时被叫.但是,从文本字段中退出第一个响应者并在UISearchBar实例中调用它们resignFirstResponder都不会使键盘消失.

当用户轻敲X按钮时,有没有办法摆脱键盘?

这是我为了获得'清除'通知而做的事情:

- (void)viewDidLoad:
{
    for (UIView* v in searchBar.subviews)
    {
        if ( [v isKindOfClass: [UITextField class]] )
        {
            UITextField *tf = (UITextField *)v;
            tf.delegate = self;
            break;
        }
    }    
}
Run Code Online (Sandbox Code Playgroud)

然后我有我的类设置来实现UISearchBarDelegate和UITextFieldDelegate.

将该类作为文本字段委托允许我接听此调用:

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
     [textField resignFirstResponder];
     [self.searchBar resignFirstResponder];
     return YES;
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有我能想到的东西.我正在尝试的最后一件事是找到一种方法来发出UISearchDelegate将在我的Controller类上调用的'searchBarCancelButtonClicked',但不是我确定我怎么能这样做,因为UISearchBar似乎没有任何直接方法使用此名称调用.

iphone uitextfield uisearchbar

18
推荐指数
4
解决办法
4万
查看次数

制作UIImage的深层副本

我的类包含一个UIImage属性,我想通过访问它的任何外部客户端强制它作为"复制"属性.但是,当我尝试在我的自定义setter中执行复制时,我得到关于UIImage不支持copyWithZone的运行时错误.那么,确保遵循正确的所有权政策的好方法是什么?

// declared in the interface as:
@property (nonatomic, readonly, copy) UIImage *personImage;

// class implementation
- (void)setPersonImage:(UIImage *)newImage
{
    if (newImage != personImage) 
    {
        [personImage release];

        // UIImage doesn't support copyWithZone
        personImage = [newImage copy];
    }
}
Run Code Online (Sandbox Code Playgroud)

iphone memory-management objective-c

13
推荐指数
2
解决办法
7641
查看次数

点击'X'元素时触发UISearchBar和事件

在UISearchBar上,有一个X元素允许您一次清除所有内容.有没有办法在发生这种情况时收到通知?

UISearchBarDelegate::searchBarCancelButtonClicked 仅在点击"取消"按钮时触发.

iphone uisearchbar uisearchbardelegate

9
推荐指数
1
解决办法
2万
查看次数

UITableView的indexPathsForVisibleRows不正确?

当我用' indexPathsForVisibleRows' 检查表的可见indexPaths 时UIScrollViewDelegate's scrollViewDidScroll and scrollViewDidEndDragging,它似乎是准确的.

但有时滚动和拖动结束和"正当cellForRowAtIndexPath"被调用,调用" indexPathsForVisibleRows"返回0.有行可见屏幕上,所以我知道它不能为0.一旦出现这种情况,我可以调用UITableView::visibleCells和得到一个非零值.

为什么存在这种差异?

iphone uitableview

8
推荐指数
1
解决办法
6429
查看次数

将光标移动到UITextField的开头

有没有办法让光标位于UITextField的开头?

当我用内容显示控件时,光标位于字符串的末尾.我想把它移到开头.

iphone uitextfield

6
推荐指数
2
解决办法
9942
查看次数

NSMutableArray的insertObjects:atIndexes和索引超出边界

有没有办法让NSMutableArray允许你在数组中有"漏洞",只有部分数组被填充而不是有连续的项目?

我想创建初始数组,并在数组的开头填充一些元素.一段时间之后,我想插入9个新项目,而不是在我当前的最后一个项目之后,但是在阵列中间的某个点,因此在数组列表的某些部分留下了一个洞.

我以为我可以这样做,但我收到错误:

NSRangeException', reason: '***
-[NSMutableArray insertObject:atIndex:]: index 28 beyond bounds [0 .. 5]'
Run Code Online (Sandbox Code Playgroud)

码:

NSMutableArray *targetArray = [NSMutableArray arrayWithCapacity:100];

- (void)fillInInitially {

// Add the first set of elements to the beginning of the array
    for (int i = 0; i < [initialData count]; ++i)
    {
        [targetArray insertObject:[initialData objectAtIndex:i] atIndex:i];
    }
}

- (void)fillInTheBlank:(NSArray *) additions {

    // Start adding at index position 28 and current array has 5 items
    NSRange range = NSMakeRange(28, [additions count]);     
    NSIndexSet *indexSet …
Run Code Online (Sandbox Code Playgroud)

iphone

5
推荐指数
1
解决办法
8752
查看次数

在XCode中获取实例的十六进制地址

有没有办法通过NSLog在XCode的调试窗口中获取实例对象的十六进制地址?

我有一组NSArray和NSDictionary对象,我想获取实例的地址,而不是内容.

尝试[obj description]会告诉我内容但不是地址.

iphone objective-c

3
推荐指数
1
解决办法
1578
查看次数

如何确定Objective-C中的最大长值?

您如何long在Objective-C中的应用程序中找到系统支持的最大值?也就是说,我们如何确定最大值long可以代表什么?

iphone macos objective-c

3
推荐指数
1
解决办法
5718
查看次数

使用va_list并获取EXC_BAD_ACCESS

类似于NSLog如何获取变量参数列表,我想创建自己的方法.

我有我的方法声明,但当我尝试访问'args'变量时,我得到一个EXEC_BAD_ACCESS.那是什么我在这里做得不对?

- (void)info:(NSString *)formatString, ...
{   
    va_list args;
    va_start(args, formatString);

    NSLog(@"formatString value: %@", formatString);

    // The following line causes the EXEC_BAD_ACCESS
    NSLog(@"args value: %@", args);

    // This is what I'm trying to do:
    NSLog(formatString, args);

    va_end(args);               
}
Run Code Online (Sandbox Code Playgroud)

我在这篇博客中关注了"Cocoa中的'va_list"部分:http: //cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html

iphone objective-c

2
推荐指数
1
解决办法
2172
查看次数