• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 用 UITextField 替换 UiNavigationItem.Title 的错误布局

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:45:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我不会用 UITextfield 替换 UINavigationItem.title。

为了做到这一点,我在 viewDidLoad 中添加了 TextField

UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
textField.text = @"";
textField.placeholder = @"set title here";
textField.font = [UIFont fontWithName"Futura-Medium" size:19.0];
textField.textColor = [UIColor whiteColor];
textField.textAlignment = NSTextAlignmentCenter;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.delegate = self;

self.navigationItem.titleView = textField;

但我在执行 TextField 的定位和调整大小时遇到​​问题:

portrait landscape

我希望 TextField 的行为类似于 NavigationItem.title

有什么想法吗?

先谢谢了

---这是我的课---

#import "CustomCategoryViewController.h"

@interface CustomCategoryViewController ()<UITextFieldDelegate>

@end

@implementation CustomCategoryViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

  NSLog(@"%f",self.navigationController.navigationItem.titleView.frame.size.height);

UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
textField.text = @"";
textField.placeholder = @"set title here";
textField.font = [UIFont fontWithName"Futura-Medium" size:19.0];
textField.textColor = [UIColor whiteColor];
textField.textAlignment = NSTextAlignmentCenter;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.delegate = self;

self.navigationItem.titleView = textField;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TextField Delegate

- (BOOL)textFieldUITextField *)textField shouldChangeCharactersInRangeNSRange)range replacementStringNSString *)string
{
    NSCharacterSet  *set = [NSCharacterSet newlineCharacterSet];

    if ([string rangeOfCharacterFromSet:[set invertedSet]].location == NSNotFound){

        [textField endEditing:YES];
    }

    return YES;
}

- (BOOL)textFieldShouldClearUITextField *)textField
{
    //self.commonName = @"";
    return YES;
}

- (void)textFieldDidEndEditingUITextField *)textField
{
    if (textField.text && ![textField.text isEqualToString""]) {
        CGRect rect = [textField textRectForBounds:textField.bounds];
        [textField setBounds:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    }
    else{
        CGRect rect = [textField placeholderRectForBounds:textField.bounds];
        [textField setBounds:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    }
}

#pragma mark - Keyboard

- (void)touchesBeganNSSet *)touches withEventUIEvent *)event
{
    [self.view endEditing:YES];
}

- (IBAction)textFieldReturnid)sender
{
    [sender resignFirstResponder];
}

#pragma mark - Interface Orientation


- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)viewWillTransitionToSizeCGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    // Code here will execute before the rotation begins.
    // Equivalent to placing it in the deprecated method -[willRotateToInterfaceOrientation:duration:]

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        // Place code here to perform animations during the rotation. You can leave this block empty if not necessary.
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
            //Background Thread
            dispatch_async(dispatch_get_main_queue(), ^(void){

            });
        });

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        // Code here will execute after the rotation has finished.
        // Equivalent to placing it in the deprecated method -[didRotateFromInterfaceOrientation:]

    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end



Best Answer-推荐答案


目前,您的 textField 宽度为 150,并且其内容保持在中心,因此当您使用长 barButton 时,您会看到文本 float 到一侧。

您需要在每次编辑 textField 后设置 textField 边界以适应 textplaceHolder喜欢

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.text && ![textField.text isEqualToString""]) {
         CGRect textRect = [textField.text boundingRectWithSize:CGSizeMake(300,30)
                                options:NSStringDrawingUsesLineFragmentOrigin
                                attributes{NSFontAttributeName:textField.font}
                                context:nil];

         [textField setBounds:CGRectMake(0, 0, textRect.size.width, textRect.size.height)];
    }
    else{
         CGRect textRect = [textField.placeholder boundingRectWithSize:CGSizeMake(300,30)
                                options:NSStringDrawingUsesLineFragmentOrigin
                                attributes{NSFontAttributeName:textField.font}
                                context:nil];

         [textField setBounds:CGRectMake(0, 0, textRect.size.width, textRect.size.height)];
    }
}

在此之后,您的 textField 将精确调整其内容的大小,并将保持在 navigationBar 的中心。

关于ios - 用 UITextField 替换 UiNavigationItem.Title 的错误布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28961592/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap