iOS round buttons and pictures on the top of the title in the Next button implementation

Source: Internet
Author: User

1 implementation of the Round button (the Click area is also circular)

The system UIButton comes in a rectangular way, and by setting the layer layer you can crop the picture to a circle, but the click area is still a rectangle. To solve this problem, the method of this article is to create a UIButton subclass, overriding the UIButton-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) method of the event. The code is as follows:

1-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *)Event2 {3BOOL flag = [Super Pointinside:point withevent:Event];4     if(flag) {5Uibezierpath *path =[Uibezierpath bezierPathWithOvalInRect:self.bounds];6         if([path Containspoint:point]) {7             returnYES;8         }9         returnNO;Ten     } One     returnNO; A}

The idea of this method is to intercept the touch-click event Response function, use Uibezierpath to set the click Area, and not handle events outside the click Area.

2 Pictures again on the implementation of the title under button

iOS comes with the UIButton system style: The picture is on the left, the title is on the right, sometimes the demand is the picture again, the title again. If you redefine a view, the corresponding event should be implemented on its own, too complicated. The method of this article is to create a UIButton subclass that overrides the two system methods of UIButton. Specifically:-(CGRect) Imagerectforcontentrect: (CGRect) Contentrect and-(CGRect) Titlerectforcontentrect: (CGRect) ContentRect. By keyword, you can see that the former overrides the position of the graphic in the button, which overrides the position of the caption in the button. The code is as follows:

1-(CGRect) Imagerectforcontentrect: (cgrect) Contentrect2 {3CGFloat Imagey =2.5;4CGFloat Imagew = -;5CGFloat Imageh = -;6CGFloat ImageX = (Contentrect.size.width-imagew)/2.0;7     8CGRect Imagerect =CGRectMake (ImageX, Imagey, Imagew, Imageh);9     returnImagerect; One } -  --(CGRect) Titlerectforcontentrect: (cgrect) Contentrect the { -CGFloat Titlex =0; -CGFloat Titley = -; -CGFloat Titlew =ContentRect.size.width; +CGFloat Titleh = contentRect.size.height-Titley; -      +CGRect Titlerect =CGRectMake (Titlex, Titley, Titlew, Titleh); at     returnTitlerect; - } -  -- (void) Setcomparebtnattribute - { - [self settitlecolor:timercolor forstate:uicontrolstatenormal]; in [self Settitlecolor:[uicolor lightgraycolor] forstate:uicontrolstatehighlighted]; -      toSelf.titleLabel.font = [Uifont systemfontofsize: One]; +Self.titleLabel.textAlignment =Nstextalignmentcenter; -      the}

The view layout takes a fixed approach, and the third function is to set the same properties for multiple buttons of the same type.

3 No, follow up. Post your source code on GitHub, and welcome to the exchange. Original, without permission, no reprint.

iOS round buttons and pictures on the top of the title in the Next button implementation

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.