IOS-set the size of the button in the middle title Position of the Weibo navigation bar --- setFrame ----, frame of the buttons in the ios navigation bar

Source: Internet
Author: User

IOS-set the size of the button in the middle title Position of the Weibo navigation bar --- setFrame ----, frame of the buttons in the ios navigation bar

# Import "HWTitleButton. h"

# Define HWMargin 5

@ Implementation HWTitleButton

-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self ){
[Self setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
Self. titleLabel. font = [UIFont boldSystemFontOfSize: 17];
[Self setImage: [UIImage imageNamed: @ "navigationbar_arrow_down"] forState: UIControlStateNormal];
[Self setImage: [UIImage imageNamed: @ "navigationbar_arrow_up"] forState: UIControlStateSelected];
}
Return self;
}

// Purpose: Modify the size after the system calculates and sets the button size.
/**
* Rewrite setFrame: Method purpose: intercept the process of setting button size
* If you want to modify the widget size after the system has set it, and make sure the modification is successful, it is generally set in setFrame:
*/
-(Void) setFrame :( CGRect) frame // to thoroughly modify the widget size, you must modify it based on setFrame.
{
Frame. size. width + = HWMargin; // Add HWMargin to the sent width; add it before [super setFrame: frame;

// Frame. origin. x + = 10;
// Frame. origin. y + = 20;


[Super setFrame: frame];
}

-(Void) layoutSubviews
{
[Super layoutSubviews];
// If you only want to adjust the positions of titleLabel and imageView in the button, set the positions separately in layoutSubviews.

// 1. Calculate the frame of titleLabel
Self. titleLabel. x = self. imageView. x;

// 2. Calculate the frame of the imageView. The Arrow picture is placed in the imageView.
Self. imageView. x = CGRectGetMaxX (self. titleLabel. frame) + HWMargin;
}

-(Void) setTitle :( NSString *) title forState :( UIControlState) state
{
[Super setTitle: title forState: state];

// As long as the text is modified, let the button recalculate its size
[Self sizeToFit];
}

-(Void) setImage :( UIImage *) image forState :( UIControlState) state
{
[Super setImage: image forState: state];

// As long as the image is modified, let the button recalculate its size
[Self sizeToFit];
}
@ End

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.