菜鸟教程小白 发表于 2022-12-13 05:45:47

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


                                            <p><p>我不会用 UITextfield 替换 UINavigationItem.title。</p>

<p>为了做到这一点,我在 viewDidLoad 中添加了 TextField</p>

<pre><code>UITextField *textField = [initWithFrame:CGRectMake(0, 0, 150, 30)];
textField.text = @&#34;&#34;;
textField.placeholder = @&#34;set title here&#34;;
textField.font = ;
textField.textColor = ;
textField.textAlignment = NSTextAlignmentCenter;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.delegate = self;

self.navigationItem.titleView = textField;
</code></pre>

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

<p> <img src="/image/mBhqL.png" alt="portrait"/> <img src="/image/px6xP.png" alt="landscape"/> </p>

<p>我希望 TextField 的行为类似于 NavigationItem.title </p>

<p>有什么想法吗? </p>

<p>先谢谢了</p>

<p>---这是我的课---</p>

<pre><code>#import &#34;CustomCategoryViewController.h&#34;

@interface CustomCategoryViewController ()&lt;UITextFieldDelegate&gt;

@end

@implementation CustomCategoryViewController

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

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

UITextField *textField = [initWithFrame:CGRectMake(0, 0, 150, 30)];
textField.text = @&#34;&#34;;
textField.placeholder = @&#34;set title here&#34;;
textField.font = ;
textField.textColor = ;
textField.textAlignment = NSTextAlignmentCenter;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.delegate = self;

self.navigationItem.titleView = textField;
}


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

#pragma mark - TextField Delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet*set = ;

    if (].location == NSNotFound){

      ;
    }

    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    //self.commonName = @&#34;&#34;;
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.text &amp;&amp; !) {
      CGRect rect = ;
      ;
    }
    else{
      CGRect rect = ;
      ;
    }
}

#pragma mark - Keyboard

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    ;
}

- (IBAction)textFieldReturn:(id)sender
{
    ;
}

#pragma mark - Interface Orientation


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id&lt;UIViewControllerTransitionCoordinator&gt;)coordinator
{
    ;

    // Code here will execute before the rotation begins.
    // Equivalent to placing it in the deprecated method -

    [coordinator animateAlongsideTransition:^(id&lt;UIViewControllerTransitionCoordinatorContext&gt; 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&lt;UIViewControllerTransitionCoordinatorContext&gt; context) {

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

    }];
}
/*
#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 .
    // Pass the selected object to the new view controller.
}
*/

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>目前,您的 <code>textField</code> 宽度为 150,并且其内容保持在中心,因此当您使用长 <code>barButton</code> 时,您会看到文本 float 到一侧。 </p>

<p>您需要在每次编辑 <code>textField</code> 后设置 <code>textField</code> 边界以适应 <code>text</code> 或 <code>placeHolder</code>喜欢</p>

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

         ;
    }
    else{
         CGRect textRect = [textField.placeholder boundingRectWithSize:CGSizeMake(300,30)
                              options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:textField.font}
                              context:nil];

         ;
    }
}
</code></pre>

<p>在此之后,您的 <code>textField</code> 将精确调整其内容的大小,并将保持在 <code>navigationBar</code> 的中心。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 用 UITextField 替换 UiNavigationItem.Title 的错误布局,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/28961592/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/28961592/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 用 UITextField 替换 UiNavigationItem.Title 的错误布局